Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get the Map as it where for JSON or XML #22

Open
genuinefafa opened this issue Sep 15, 2014 · 10 comments
Open

Get the Map as it where for JSON or XML #22

genuinefafa opened this issue Sep 15, 2014 · 10 comments

Comments

@genuinefafa
Copy link

I'm using this plugin and sometimes I need the Map equivalent for a JSON marshaling. Off course, I dont want it to be JSON, but a real Groovy Map.

Is there any way to make that? I know that internally the plugin does a Map to work on (#21).

@dhalupa
Copy link
Collaborator

dhalupa commented Sep 16, 2014

On 16.09.2014 00:40, genuinefafa wrote:

I'm using this plugin and sometimes I need the Map equivalent for a
JSON marshaling. Off course, I dont want it to be JSON, but a real
Groovy Map.

Is there any way to make that? I know that internally the plugin does
a Map to work on (#21
#21).


Reply to this email directly or view it on GitHub
#22.

I'm not sure I understood your requirements. What do you mean by 'Map
equivalent'?

@genuinefafa
Copy link
Author

English is not my native lang. I'm sorry! 😄

If I do a marshaling of the object, I get a JSON representation of the data contained in the object. In other words, the properties and not the methods. That JSON representation can be converted to a Groovy Map.

What I need/want is a Groovy Map as it were marshaled so I can do my own "magic" there.

@dhalupa
Copy link
Collaborator

dhalupa commented Sep 16, 2014

On 16.09.2014 20:21, genuinefafa wrote:

English is not my native lang. I'm sorry! 😄

mine neither :)

If I do a marshaling of the object, I get a JSON representation of the
data contained in the object. In other words, the properties and not
the methods. That JSON representation can be converted to a Groovy Map.

What I need/want is a Groovy Map as it were marshaled so I can do my
own "magic" there.

ok, now I understand. no, it is not possible


Reply to this email directly or view it on GitHub
#22 (comment).

@genuinefafa
Copy link
Author

I think it can be a nice feature to add, since it allow treatment of the object as it were received as JSON.

I can do the development of the feature, just point my in the right direction. I belive it should be easy.

@dhalupa
Copy link
Collaborator

dhalupa commented Sep 16, 2014

On 16.09.2014 20:43, genuinefafa wrote:

I think it can be a nice feature to add, since it allow treatment of
the object as it were received as JSON.

Frankly, I can not see the use case for such feature. Could you more
thoroughly describe your requirements?

I can do the development of the feature, just point my in the right
direction. I belive it should be easy.


Reply to this email directly or view it on GitHub
#22 (comment).

@genuinefafa
Copy link
Author

Remote Computer RT
Server Computer SC

Case 1

RT request for a data
SC marshalls data in JSON
RT publish data using the JSON received from SC
SC receives the data and saves in a NoSQL DB exactly the received JSON as a Groovy Map.

Case 2

This cases tries to repeat exactly the same behavior without using a remote computer
SC reads the data
SC convert the data to Map (hint: I need the Object->JSON converter here)
SC saves in a NoSQL DB exactly what it was in the Map

There you are.

@dhalupa
Copy link
Collaborator

dhalupa commented Sep 16, 2014

On 16.09.2014 20:50, genuinefafa wrote:

Remote Computer RT
Server Computer SC

  Case 1

RT request for a data
SC marshalls data in JSON
RT publish data using the JSON received from SC
SC receives the data and saves in a NoSQL DB exactly the received JSON
as a Groovy Map.

  Case 2

This cases tries to repeat exactly the same behavior without using a
remote computer
SC reads the data
SC convert the data to Map (hint: I need the Object->JSON converter here)
SC saves in a NoSQL DB exactly what it was in the Map

There you are.


Reply to this email directly or view it on GitHub
#22 (comment).

ok, I can see what you mean. However, this is not the scope of
marshallers plugin. GORM offers nice support for converting domain
objects to map instances. you should use that

@vahidpaz
Copy link

genuinefafa,

I'm not sure I fully understand your requirements, but maybe this will help. I've not tested this code myself so I'm sure there are bugs :)

String jsonText = '{ "foo": "bar" }'
def jsonDeserialized = grails.converters.JSON.parse(jsonText)
assert jsonDeserialized instanceof Map
assert jsonDeserialized instanceof org.codehaus.groovy.grails.web.json.JSONElement

Or if you are not using Grails:

String jsonText = '{ "foo": "bar" }'
def jsonDeserialized = new groovy.json.JsonSlurper().parseText(jsonText)
assert jsonDeserialized instanceof Map
assert jsonDeserialized instanceof org.codehaus.groovy.grails.web.json.JSONElement

A more convenient way to do this in Grails is to use the "request" object which is automatically injected into controllers for you, or you can retrieve it from anywhere via WebUtils.retrieveGrailsWebRequest().request:

// Reads request body for you
def jsonDeserialized = request.JSON

assert jsonDeserialized instanceof Map
assert jsonDeserialized instanceof org.codehaus.groovy.grails.web.json.JSONElement

I'm not sure how to directly convert an arbitrary object to nested maps/lists without a little more work.

class MyDomainClass { String p1; int p2 }

def obj = new MyDomainClass(p1: 'foo', p2: 10)
def data = obj.properties

assert data instanceof Map

If MyDomainClass is flat (i.e., no other domain class references inside MyDomainClass) then the above will suffice. If it's not flat and you want to end up with nested maps, then you need to walk the object tree yourself, and for each nested object use the ".properties" notation to create a map.

A different option which I won't really recommend is this roundabout technique. Maybe you can learn something from it:

def obj = new MyDomainClass(p1: 'foo', p2: 10)
String jsonText = new JsonBuilder(obj).toPrettyString()
def data = new JsonSlurper().parseText(jsonText)

assert data instanceof Map

@vahidpaz
Copy link

You also might want to look at:

org.codehaus.groovy.grails.web.binding.DataBindingUtils.bind*()

@genuinefafa
Copy link
Author

@vahidpaz DataBindingUtils is interesting.

But, I refuse to do a Domain Obj->JSON and a JSON->Map.

What I ended up doing was this:

        def domainObject = DomainObject.get(id)
        def newMap = [:]
        domainObject.domainClass.persistentProperties.each {
            if (!it.association) newMap.put it.name, domainObject."${it.name}"
        }

        def c = newMap + [ id: domainObject.id ]

But I don't really like because it doesn't honor the marshalling setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants