Quantcast

when does JsonSlurper return LazyMap vs LazyValueMap ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

when does JsonSlurper return LazyMap vs LazyValueMap ?

garneke
groovy-all-2.4.6 In my environment I am seeing that JsonSlurper returns LazyMap in my test code using the groovyConsole but it returns a LazyValueMap in my compiled java code. I am trying to add a node to the json object and it works great in the groovyConsole but fails in java. This code works in the groovyConsole... def slurper = new JsonSlurper() def json = slurper.parseText( jsonTxt ) json.put("mediaHash", mediaHash) // -- or -- json."mediaHash" = mediaHash In the groovyConsole slurper.parseText() returns a LazyMap But in Java it returns a LazyValueMap and I get the exception ... die("Not that kind of map"); What is the correct way to add a node and why do I get different results? Thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: when does JsonSlurper return LazyMap vs LazyValueMap ?

garneke
-- Sorry for the first post ( formatting was lost )

Environment: groovy-all-2.4.6

In my environment I am seeing that JsonSlurper returns LazyMap in my test code using the groovyConsole but it returns a LazyValueMap in my compiled java code.

I am trying to add a node to the json object and it works great in the groovyConsole but fails in java.

This code works in the groovyConsole...

 
 def slurper = new JsonSlurper()
 def json = slurper.parseText( jsonTxt )
 json.put("mediaHash", mediaHash)
 // -- or  --
 json."mediaHash" = mediaHash


In the  groovyConsole  slurper.parseText() returns a LazyMap
But in Java it returns a LazyValueMap and I get the exception ... die("Not that kind of map");

What is the correct way to add a node and why do I get different results?
Thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: when does JsonSlurper return LazyMap vs LazyValueMap ?

garneke

Update:

I have determined that the difference in the return type has to do with the JsonSlurper type.  The INDEX_OVERLAY returns a LazyValueMap. 

 

That being said… 

What is the correct way to add a node or otherwise modify the LazyValueMap objects?

 

 

From: garneke [via Groovy] [mailto:ml-node+[hidden email]]
Sent: Thursday, January 26, 2017 12:31 PM
To: Kenton Garner <[hidden email]>
Subject: Re: when does JsonSlurper return LazyMap vs LazyValueMap ?

 

-- Sorry for the first post ( formatting was lost )

Environment: groovy-all-2.4.6

In my environment I am seeing that JsonSlurper returns LazyMap in my test code using the groovyConsole but it returns a LazyValueMap in my compiled java code.

I am trying to add a node to the json object and it works great in the groovyConsole but fails in java.

This code works in the groovyConsole...

 
 def slurper = new JsonSlurper()
 def json = slurper.parseText( jsonTxt )
 json.put("mediaHash", mediaHash)
 // -- or  --
 json."mediaHash" = mediaHash


In the  groovyConsole  slurper.parseText() returns a LazyMap
But in Java it returns a LazyValueMap and I get the exception ... die("Not that kind of map");

What is the correct way to add a node and why do I get different results?
Thanks


If you reply to this email, your message will be added to the discussion below:

http://groovy.329449.n5.nabble.com/when-does-JsonSlurper-return-LazyMap-vs-LazyValueMap-tp5738132p5738133.html

To unsubscribe from when does JsonSlurper return LazyMap vs LazyValueMap ?, click here.
NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: when does JsonSlurper return LazyMap vs LazyValueMap ?

jwagenleitner

On Thu, Jan 26, 2017 at 12:27 PM, garneke <[hidden email]> wrote:

Update:

I have determined that the difference in the return type has to do with the JsonSlurper type.  The INDEX_OVERLAY returns a LazyValueMap. 

 

That being said… 

What is the correct way to add a node or otherwise modify the LazyValueMap objects?

 



I think the easiest way to do this if there are no nested objects is to create a new Map from the LazyValueMap.  If there are no nested objects then the following would work.


def newMap = new LinkedHashMap(lazyValueMap)
newMap.mediaHash = mediaHash


If there are nested objects then it takes more work as you'd have to recurse/iterate over each nested LazyValueMap and convert it as well.  There's an issue [1] requesting the ability to convert to a regular map.


 

 

From: garneke [via Groovy] [mailto:[hidden email][hidden email]]
Sent: Thursday, January 26, 2017 12:31 PM
To: Kenton Garner <[hidden email]>
Subject: Re: when does JsonSlurper return LazyMap vs LazyValueMap ?

 

-- Sorry for the first post ( formatting was lost )

Environment: groovy-all-2.4.6

In my environment I am seeing that JsonSlurper returns LazyMap in my test code using the groovyConsole but it returns a LazyValueMap in my compiled java code.

I am trying to add a node to the json object and it works great in the groovyConsole but fails in java.

This code works in the groovyConsole...

 
 def slurper = new JsonSlurper()
 def json = slurper.parseText( jsonTxt )
 json.put("mediaHash", mediaHash)
 // -- or  --
 json."mediaHash" = mediaHash


In the  groovyConsole  slurper.parseText() returns a LazyMap
But in Java it returns a LazyValueMap and I get the exception ... die("Not that kind of map");

What is the correct way to add a node and why do I get different results?
Thanks


If you reply to this email, your message will be added to the discussion below:

http://groovy.329449.n5.nabble.com/when-does-JsonSlurper-return-LazyMap-vs-LazyValueMap-tp5738132p5738133.html

To unsubscribe from when does JsonSlurper return LazyMap vs LazyValueMap ?, click here.
NAML



View this message in context: RE: when does JsonSlurper return LazyMap vs LazyValueMap ?

Sent from the Groovy Users mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: when does JsonSlurper return LazyMap vs LazyValueMap ?

jnorthr
A simple question here. The jsonSlurper creates a ConfigObject but i cannot find a way to add an entry to the ConfigObject with the ultimate intention of writing the whole shebang back to the json text file whence it came. Any ideas pls. .?

Sent from my iPad

On 28 Jan 2017, at 02:55, John Wagenleitner <[hidden email]> wrote:


On Thu, Jan 26, 2017 at 12:27 PM, garneke <[hidden email]> wrote:

Update:

I have determined that the difference in the return type has to do with the JsonSlurper type.  The INDEX_OVERLAY returns a LazyValueMap. 

 

That being said… 

What is the correct way to add a node or otherwise modify the LazyValueMap objects?

 



I think the easiest way to do this if there are no nested objects is to create a new Map from the LazyValueMap.  If there are no nested objects then the following would work.


def newMap = new LinkedHashMap(lazyValueMap)
newMap.mediaHash = mediaHash


If there are nested objects then it takes more work as you'd have to recurse/iterate over each nested LazyValueMap and convert it as well.  There's an issue [1] requesting the ability to convert to a regular map.


 

 

From: garneke [via Groovy] [mailto:[hidden email][hidden email]]
Sent: Thursday, January 26, 2017 12:31 PM
To: Kenton Garner <[hidden email]>
Subject: Re: when does JsonSlurper return LazyMap vs LazyValueMap ?

 

-- Sorry for the first post ( formatting was lost )

Environment: groovy-all-2.4.6

In my environment I am seeing that JsonSlurper returns LazyMap in my test code using the groovyConsole but it returns a LazyValueMap in my compiled java code.

I am trying to add a node to the json object and it works great in the groovyConsole but fails in java.

This code works in the groovyConsole...

 
 def slurper = new JsonSlurper()
 def json = slurper.parseText( jsonTxt )
 json.put("mediaHash", mediaHash)
 // -- or  --
 json."mediaHash" = mediaHash


In the  groovyConsole  slurper.parseText() returns a LazyMap
But in Java it returns a LazyValueMap and I get the exception ... die("Not that kind of map");

What is the correct way to add a node and why do I get different results?
Thanks


If you reply to this email, your message will be added to the discussion below:

http://groovy.329449.n5.nabble.com/when-does-JsonSlurper-return-LazyMap-vs-LazyValueMap-tp5738132p5738133.html

To unsubscribe from when does JsonSlurper return LazyMap vs LazyValueMap ?, click here.
NAML



View this message in context: RE: when does JsonSlurper return LazyMap vs LazyValueMap ?

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: when does JsonSlurper return LazyMap vs LazyValueMap ?

garneke
In reply to this post by jwagenleitner

Thank you that helped a lot.

BTW the issue you posted https://issues.apache.org/jira/browse/GROOVY-7532  had example code to walk the json map object and make hard references to objects ( LAX to Normal )

 

def slurpOpts = jsonSlurperLAX.parse(new File("test.conf"))

slurpOpts.each {k, v -> configOpts2.put(k, slurpOpts.get(k))}

 

I have a question about this simple example though.  If the json is more complex and has nested maps this solution would be incomplete correct?

 

I decided to use the following but I don’t know if it is overkill or even valid.  Any thoughts?

 

  public Map<String, Object> deepCloneJsonMap(Object json){

    Map<String, Object> result = new LinkedHashMap();

    json.each {k, v ->

      if( v instanceof Map )

        result.put(k, deepCloneJsonMap(((Map<String,Object>)json).get(k)))

      else

        result.put(k, ((Map<String,Object>)json).get(k))

    }

    return result;

  }

 

Thanks again.

 

 

From: jwagenleitner [via Groovy] [mailto:ml-node+[hidden email]]
Sent: Friday, January 27, 2017 8:56 PM
To: Kenton Garner <[hidden email]>
Subject: Re: when does JsonSlurper return LazyMap vs LazyValueMap ?

 

 

On Thu, Jan 26, 2017 at 12:27 PM, garneke <[hidden email]> wrote:

Update:

I have determined that the difference in the return type has to do with the JsonSlurper type.  The INDEX_OVERLAY returns a LazyValueMap. 

 

That being said… 

What is the correct way to add a node or otherwise modify the LazyValueMap objects?

 

 

 

I think the easiest way to do this if there are no nested objects is to create a new Map from the LazyValueMap.  If there are no nested objects then the following would work.

 

 

def newMap = new LinkedHashMap(lazyValueMap)

newMap.mediaHash = mediaHash

 

 

If there are nested objects then it takes more work as you'd have to recurse/iterate over each nested LazyValueMap and convert it as well.  There's an issue [1] requesting the ability to convert to a regular map.

 

 

 

 

From: garneke [via Groovy] [mailto:[hidden email][hidden email]]
Sent: Thursday, January 26, 2017 12:31 PM
To: Kenton Garner <[hidden email]>
Subject: Re: when does JsonSlurper return LazyMap vs LazyValueMap ?

 

-- Sorry for the first post ( formatting was lost )

Environment: groovy-all-2.4.6

In my environment I am seeing that JsonSlurper returns LazyMap in my test code using the groovyConsole but it returns a LazyValueMap in my compiled java code.

I am trying to add a node to the json object and it works great in the groovyConsole but fails in java.

This code works in the groovyConsole...

 
 def slurper = new JsonSlurper()
 def json = slurper.parseText( jsonTxt )
 json.put("mediaHash", mediaHash)
 // -- or  --
 json."mediaHash" = mediaHash


In the  groovyConsole  slurper.parseText() returns a LazyMap
But in Java it returns a LazyValueMap and I get the exception ... die("Not that kind of map");

What is the correct way to add a node and why do I get different results?
Thanks


If you reply to this email, your message will be added to the discussion below:

http://groovy.329449.n5.nabble.com/when-does-JsonSlurper-return-LazyMap-vs-LazyValueMap-tp5738132p5738133.html

To unsubscribe from when does JsonSlurper return LazyMap vs LazyValueMap ?, click here.
NAML

 


View this message in context: RE: when does JsonSlurper return LazyMap vs LazyValueMap ?


Sent from the Groovy Users mailing list archive at Nabble.com.

 

 


If you reply to this email, your message will be added to the discussion below:

http://groovy.329449.n5.nabble.com/when-does-JsonSlurper-return-LazyMap-vs-LazyValueMap-tp5738132p5738176.html

To unsubscribe from when does JsonSlurper return LazyMap vs LazyValueMap ?, click here.
NAML

Loading...