dclowd9901
dclowd9901

Reputation: 6826

Groovy/Grails: Use JSON to mock model data

I'm working on the front end of an application, and model data isn't yet available. I'd like to be able to mock the models that I will eventually be getting using JSON. Ideally, I'd be able to define a JSON file, have it be read and parsed into an appropriate model structure, then passed to the view as normal (so that later on, when the model data is ready, the substitution is seamless).

What's the best approach for this?

Upvotes: 0

Views: 1096

Answers (3)

gsaqui
gsaqui

Reputation: 265

In my controller I would do something like the following:

def map = [:]
map = [name:'value1", name2:"value2"]
render map as JSON

The idea being that you should just put values in there to get you started on the frontend. If this is a heavy js application wouldn't you be better off testing your js in isolation from any backend?

Upvotes: 0

Ken Liu
Ken Liu

Reputation: 22914

Maybe the Grails Fixtures plugin would be helpful? It's really intended to be used for loading test data for automated tests, but it could be used for loading sample data as well.

Otherwise Groovy has some nice classes for dealing with JSON like JsonSlurper that you could use for creating stub service implementations.

Upvotes: 1

natepers
natepers

Reputation: 533

Sounds like what you want to do is just create some JSON manually in the shape of what you expect to have the final data.

If you expect a lot of JSON and it's impractical to produce it by hand then you can give a dummy JSON generator to see if there is anything out there that will allow you to create some dummy data in the same shape you expect:

This one is pretty cool and let's you define a structure which it fills with data:

http://json-generator.appspot.com/

If you like a graphical approach you can try out http://jsoneditor.net/ which lets you build JSON structures in your browser.

Upvotes: 1

Related Questions