You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by aappddeevv <aa...@verizon.net> on 2010/07/02 15:26:49 UTC

recommendation requested: json translation into objects

I have some properties on a skin that need to be translated from json syntax
to an object. Pivot seems to have a few translators for colors and paints
for its needs.


However, I have some other structures to convert.

 

What recommendation do people have for converting json syntax to an object?
Should I grab a library? If so, any suggestions on which one? 

 

Or is a library overkill and that's the whole point of json?

 

Ideally, I could register my converter and it gets called automatically when
I indicate the need for a conversion to a certain type.


Re: recommendation requested: json translation into objects

Posted by Greg Brown <gk...@mac.com>.
I should mention that, if it is not a bean, we generally provide a constructor that takes a JSON type such as Map that you can use to instantiate the object. All of Pivot's "struct" classes, including Dimensions, Point, etc., work this way so you can use them as a reference.

On Jul 2, 2010, at 9:35 AM, Greg Brown wrote:

>> What recommendation do people have for converting json syntax to an object? Should I grab a library? If so, any suggestions on which one?
> 
> If it is a bean, you can use JSONSerializer. See org.apache.pivot.tests.JSONBeanTest for an example.
> 
>> Ideally, I could register my converter and it gets called automatically when I indicate the need for a conversion to a certain type.
> 
> Generally, we handle this by providing a string setter that performs the conversion and calls the typed setter.
> 
> 


Re: recommendation requested: json translation into objects

Posted by Greg Brown <gk...@mac.com>.
FYI, org.apache.pivot.tutorials.webqueries.server.ExpenseServlet also uses this technique.

On Jul 2, 2010, at 9:54 AM, aappddeevv wrote:

>  
>  
> From: Greg Brown [mailto:gkbrown@mac.com] 
> Sent: Friday, July 02, 2010 9:36 AM
> To: user@pivot.apache.org
> Subject: Re: recommendation requested: json translation into objects
>  
> What recommendation do people have for converting json syntax to an object? Should I grab a library? If so, any suggestions on which one?
>  
> If it is a bean, you can use JSONSerializer. See org.apache.pivot.tests.JSONBeanTest for an example.
>  
>  
> >>>Hmm…looked at the JSONSerializer again. I think it can work.  I looked at the test, but my needs are from a JSON string to an object. The test showed the object to string version.  Based on your suggestion, I would need something like  below. I’ll see how robust this is over time. Thanks!
>  
> BeanAdapter ba = new BeanAdapter(myObject);
> theMap = JSONSerializer.toMap(myJsonSTring)
> ba.putAll(theMap)
>  
>  
> Ideally, I could register my converter and it gets called automatically when I indicate the need for a conversion to a certain type.
>  
> Generally, we handle this by providing a string setter that performs the conversion and calls the typed setter.
>  
> >>>Yup. I do this.  On a separate note, this is also a motivation for potentially recommending a PivotStyle annotation so I can find all the styles more easily. If multiple getters exist, its difficult to programmatically scan and dedup and know the true style object type—something useful for tooling. I still have to read javadoc to get styling information.


Re: recommendation requested: json translation into objects

Posted by Greg Brown <gk...@mac.com>.
> >>>Yup. I do this.  On a separate note, this is also a motivation for potentially recommending a PivotStyle annotation so I can find all the styles more easily. If multiple getters exist, its difficult to programmatically scan and dedup and know the true style object type—something useful for tooling. I still have to read javadoc to get styling information.

This seems like a decent idea. Can you create a JIRA ticket for it?



Re: recommendation requested: json translation into objects

Posted by Greg Brown <gk...@mac.com>.
Oops, you're right. The example you should look at is org.apache.pivot.json.test.BindTest.


On Jul 2, 2010, at 9:54 AM, aappddeevv wrote:

>  
>  
> From: Greg Brown [mailto:gkbrown@mac.com] 
> Sent: Friday, July 02, 2010 9:36 AM
> To: user@pivot.apache.org
> Subject: Re: recommendation requested: json translation into objects
>  
> What recommendation do people have for converting json syntax to an object? Should I grab a library? If so, any suggestions on which one?
>  
> If it is a bean, you can use JSONSerializer. See org.apache.pivot.tests.JSONBeanTest for an example.
>  
>  
> >>>Hmm…looked at the JSONSerializer again. I think it can work.  I looked at the test, but my needs are from a JSON string to an object. The test showed the object to string version.  Based on your suggestion, I would need something like  below. I’ll see how robust this is over time. Thanks!
>  
> BeanAdapter ba = new BeanAdapter(myObject);
> theMap = JSONSerializer.toMap(myJsonSTring)
> ba.putAll(theMap)
>  
>  
> Ideally, I could register my converter and it gets called automatically when I indicate the need for a conversion to a certain type.
>  
> Generally, we handle this by providing a string setter that performs the conversion and calls the typed setter.
>  
> >>>Yup. I do this.  On a separate note, this is also a motivation for potentially recommending a PivotStyle annotation so I can find all the styles more easily. If multiple getters exist, its difficult to programmatically scan and dedup and know the true style object type—something useful for tooling. I still have to read javadoc to get styling information.


RE: recommendation requested: json translation into objects

Posted by aappddeevv <aa...@verizon.net>.
 

 

From: Greg Brown [mailto:gkbrown@mac.com] 
Sent: Friday, July 02, 2010 9:36 AM
To: user@pivot.apache.org
Subject: Re: recommendation requested: json translation into objects

 

What recommendation do people have for converting json syntax to an object?
Should I grab a library? If so, any suggestions on which one?

 

If it is a bean, you can use JSONSerializer. See
org.apache.pivot.tests.JSONBeanTest for an example.

 

 

>>>Hmm.looked at the JSONSerializer again. I think it can work.  I looked at
the test, but my needs are from a JSON string to an object. The test showed
the object to string version.  Based on your suggestion, I would need
something like  below. I'll see how robust this is over time. Thanks!

 

BeanAdapter ba = new BeanAdapter(myObject);

theMap = JSONSerializer.toMap(myJsonSTring)

ba.putAll(theMap)

 

 

Ideally, I could register my converter and it gets called automatically when
I indicate the need for a conversion to a certain type.

 

Generally, we handle this by providing a string setter that performs the
conversion and calls the typed setter.

 

>>>Yup. I do this.  On a separate note, this is also a motivation for
potentially recommending a PivotStyle annotation so I can find all the
styles more easily. If multiple getters exist, its difficult to
programmatically scan and dedup and know the true style object
type-something useful for tooling. I still have to read javadoc to get
styling information.


Re: recommendation requested: json translation into objects

Posted by Greg Brown <gk...@mac.com>.
> What recommendation do people have for converting json syntax to an object? Should I grab a library? If so, any suggestions on which one?

If it is a bean, you can use JSONSerializer. See org.apache.pivot.tests.JSONBeanTest for an example.

> Ideally, I could register my converter and it gets called automatically when I indicate the need for a conversion to a certain type.

Generally, we handle this by providing a string setter that performs the conversion and calls the typed setter.



RE: recommendation requested: json translation into objects

Posted by aappddeevv <aa...@verizon.net>.
Oh yes. I'm using it that way now, that's what motivated a JIRA to add
putAll to the BeanAdapter J

 

From: Greg Brown [mailto:gkbrown@mac.com] 
Sent: Saturday, July 03, 2010 8:06 AM
To: user@pivot.apache.org
Subject: Re: recommendation requested: json translation into objects

 

FWIW, JSONSerializer can also easily convert JSON strings to lists or maps:

 

List<?> list = JSONSerializer.parseList("[1, 2, 3, 4, 5]");

Map<String, ?> map = JSONSerializer.parseMap("{a:1, b:2, c:3}");

 

It will also automatically deserialize the data into custom classes if you
pass the constructor an appropriate type.





Google gson? I think that's a clever name. Gson looks like it smartly
attacks this problem. I love the fact it supports generics. Thanks for the
reference.

I've had good experiences with Google's Gson:

http://code.google.com/p/google-gson/

though I don't know if it would be suitable here.  It has some nice
features, like being able to turn [1,2,3,4,5] into a
java.util.List<Integer>.  It can also do things like turn
[{'x':1},{'x':2},{'x':3},{'x':4},{'x':5}] into a List<Foo>, if you have

class Foo {
   int x;
}

 


Re: recommendation requested: json translation into objects

Posted by Greg Brown <gk...@mac.com>.
FWIW, JSONSerializer can also easily convert JSON strings to lists or maps:

List<?> list = JSONSerializer.parseList("[1, 2, 3, 4, 5]");
Map<String, ?> map = JSONSerializer.parseMap("{a:1, b:2, c:3}");

It will also automatically deserialize the data into custom classes if you pass the constructor an appropriate type.

> Google gson? I think that's a clever name. Gson looks like it smartly
> attacks this problem. I love the fact it supports generics. Thanks for the
> reference.
> 
>> I've had good experiences with Google's Gson:
>> 
>> http://code.google.com/p/google-gson/
>> 
>> though I don't know if it would be suitable here.  It has some nice
>> features, like being able to turn [1,2,3,4,5] into a
>> java.util.List<Integer>.  It can also do things like turn
>> [{'x':1},{'x':2},{'x':3},{'x':4},{'x':5}] into a List<Foo>, if you have
>> 
>> class Foo {
>>    int x;
>> }


RE: recommendation requested: json translation into objects

Posted by aappddeevv <aa...@verizon.net>.
Google gson? I think that's a clever name. Gson looks like it smartly
attacks this problem. I love the fact it supports generics. Thanks for the
reference.


I think the small code snippet in an earlier response helps, the only
problem is that some objects don't serialize/deserialize very well (no fault
of pivot) and I want to have custom translation much like pivot does with
paint deserialization from a json string so that I have a simpler-than-xml
way to represent some objects and markup, specifically around affine
transformations for the SVG component I recently posted.

For my single object needs, I'll do it like pivot with a single converter,
although it would be nice if pivot had a ConverterService to be called by
any component to translate from json string to an object...pivot could load
its converters and other people could write theirs and contribute them with
a standard mechanism.  But is not an essential service to pivot today.


-----Original Message-----
From: Clint Gilbert [mailto:clint_gilbert@hms.harvard.edu] 
Sent: Friday, July 02, 2010 10:08 AM
To: user@pivot.apache.org
Subject: Re: recommendation requested: json translation into objects

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

aappddeevv wrote:
> I have some properties on a skin that need to be translated from json
syntax to an object. Pivot seems to have a few translators for colors and
paints for its needs.
> 
> However, I have some other structures to convert.
> 
> What recommendation do people have for converting json syntax to an
object? Should I grab a library? If so, any suggestions on which one?

I've had good experiences with Google's Gson:

http://code.google.com/p/google-gson/

though I don't know if it would be suitable here.  It has some nice
features, like being able to turn [1,2,3,4,5] into a
java.util.List<Integer>.  It can also do things like turn
[{'x':1},{'x':2},{'x':3},{'x':4},{'x':5}] into a List<Foo>, if you have

class Foo {
    int x;
}


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkwt8s4ACgkQ5IyIbnMUeTvMNgCfZ3XypqFQ1oymgPPHj/co3V95
Lp4AoKlUu7quASTbkod9WF63m53BD0eM
=2l5G
-----END PGP SIGNATURE-----


Re: recommendation requested: json translation into objects

Posted by Clint Gilbert <cl...@hms.harvard.edu>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

aappddeevv wrote:
> I have some properties on a skin that need to be translated from json syntax to an object. Pivot seems to have a few translators for colors and paints for its needs.
> 
> However, I have some other structures to convert.
> 
> What recommendation do people have for converting json syntax to an object? Should I grab a library? If so, any suggestions on which one?

I've had good experiences with Google's Gson:

http://code.google.com/p/google-gson/

though I don't know if it would be suitable here.  It has some nice
features, like being able to turn [1,2,3,4,5] into a
java.util.List<Integer>.  It can also do things like turn
[{'x':1},{'x':2},{'x':3},{'x':4},{'x':5}] into a List<Foo>, if you have

class Foo {
    int x;
}


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkwt8s4ACgkQ5IyIbnMUeTvMNgCfZ3XypqFQ1oymgPPHj/co3V95
Lp4AoKlUu7quASTbkod9WF63m53BD0eM
=2l5G
-----END PGP SIGNATURE-----