Upgrading json-lib

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Upgrading json-lib

byersa
Has anyone else felt a need to update the version of json-lib? It is in
webapp and is at 0.9, whereas I am at version 2.2.1 for jdk1.5 (there is a
separate version for 1.3 ???). As important as json will as more and more
work in done with ajax, it would seem that an upgrade would be in order.
There are only two places I see that would be affects - in two event
handlers that use JSONObject.fromMap where the new syntax is
JSONObject.fromObject.

One important thing that the  latest version brings is a plug-in system for
dealing with complex object, primarily date fields. I will post more about
this below. I realize that having two versions for 1.5 and 1.3 is not
desirable. The 1.3 version is upwards compatible, so I suppose it is the way
to go.

-Al

For anyone who stumbles on the email searching for json info, I am going to
paste in a post I made on the Dojo site, just in case it is helpful.

...

json-lib has a way to plug-in support for custom conversions.

Here is the code that I use to "plug-in" custom support:
...
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor( java.sql.Timestamp.class, new
TimestampJsonValueProcessor() );
boolean isMap = serviceContext instanceof Map;
JSONObject json = JSONObject.fromObject(serviceContext, jsonConfig);
String jsonStr = json.toString();
...
and here is the code for TimestampJsonValueProcessor:

public class TimestampJsonValueProcessor implements JsonValueProcessor {

public TimestampJsonValueProcessor() {
}

public Object processArrayValue( Object value, JsonConfig jsonConfig ) {
return process( value, jsonConfig );
}

public Object processObjectValue( String key, Object value, JsonConfig
jsonConfig ) {
return process( value, jsonConfig );
}

private Object process( Object value, JsonConfig jsonConfig ) {
Calendar c = Calendar.getInstance();
c.setTime( (Date) value );
JSONObject jsonObjectValue = new JSONObject().element( "year", c.get(
Calendar.YEAR ) )
.element( "month", c.get( Calendar.MONTH ) )
.element( "day", c.get( Calendar.DAY_OF_MONTH ) )
.element( "hours", c.get( Calendar.HOUR_OF_DAY ) )
.element( "minutes", c.get( Calendar.MINUTE ) )
.element( "seconds", c.get( Calendar.SECOND ) )
.element( "milliseconds", c.get( Calendar.MILLISECOND ) );

// This method works, too
JSONObject jsonObjectValue2 = JSONObject.fromObject(value, jsonConfig);
JSONObject jsonObject = new JSONObject().element("_type",
"Date").element("_value", jsonObjectValue);

return jsonObject;
}
}

Hope this helps.
...
The Dojo Toolkit has a data store object which is a nice vehicle for
communicating back and forth with the server. It is one feature that should
be evaluated in comparing javascript toolkits. The grid object is another
big one.
Reply | Threaded
Open this post in threaded view
|

Re: Upgrading json-lib

Adrian Crum
As long as it works with the latest version of Prototype, it's fine with me.

-Adrian

Al Byers wrote:

> Has anyone else felt a need to update the version of json-lib? It is in
> webapp and is at 0.9, whereas I am at version 2.2.1 for jdk1.5 (there is a
> separate version for 1.3 ???). As important as json will as more and more
> work in done with ajax, it would seem that an upgrade would be in order.
> There are only two places I see that would be affects - in two event
> handlers that use JSONObject.fromMap where the new syntax is
> JSONObject.fromObject.
>
> One important thing that the  latest version brings is a plug-in system for
> dealing with complex object, primarily date fields. I will post more about
> this below. I realize that having two versions for 1.5 and 1.3 is not
> desirable. The 1.3 version is upwards compatible, so I suppose it is the way
> to go.
>
> -Al
>
> For anyone who stumbles on the email searching for json info, I am going to
> paste in a post I made on the Dojo site, just in case it is helpful.
>
> ...
>
> json-lib has a way to plug-in support for custom conversions.
>
> Here is the code that I use to "plug-in" custom support:
> ...
> JsonConfig jsonConfig = new JsonConfig();
> jsonConfig.registerJsonValueProcessor( java.sql.Timestamp.class, new
> TimestampJsonValueProcessor() );
> boolean isMap = serviceContext instanceof Map;
> JSONObject json = JSONObject.fromObject(serviceContext, jsonConfig);
> String jsonStr = json.toString();
> ...
> and here is the code for TimestampJsonValueProcessor:
>
> public class TimestampJsonValueProcessor implements JsonValueProcessor {
>
> public TimestampJsonValueProcessor() {
> }
>
> public Object processArrayValue( Object value, JsonConfig jsonConfig ) {
> return process( value, jsonConfig );
> }
>
> public Object processObjectValue( String key, Object value, JsonConfig
> jsonConfig ) {
> return process( value, jsonConfig );
> }
>
> private Object process( Object value, JsonConfig jsonConfig ) {
> Calendar c = Calendar.getInstance();
> c.setTime( (Date) value );
> JSONObject jsonObjectValue = new JSONObject().element( "year", c.get(
> Calendar.YEAR ) )
> .element( "month", c.get( Calendar.MONTH ) )
> .element( "day", c.get( Calendar.DAY_OF_MONTH ) )
> .element( "hours", c.get( Calendar.HOUR_OF_DAY ) )
> .element( "minutes", c.get( Calendar.MINUTE ) )
> .element( "seconds", c.get( Calendar.SECOND ) )
> .element( "milliseconds", c.get( Calendar.MILLISECOND ) );
>
> // This method works, too
> JSONObject jsonObjectValue2 = JSONObject.fromObject(value, jsonConfig);
> JSONObject jsonObject = new JSONObject().element("_type",
> "Date").element("_value", jsonObjectValue);
>
> return jsonObject;
> }
> }
>
> Hope this helps.
> ...
> The Dojo Toolkit has a data store object which is a nice vehicle for
> communicating back and forth with the server. It is one feature that should
> be evaluated in comparing javascript toolkits. The grid object is another
> big one.
>