Discussion:
Question regarding json and attributes
Oded Hassidi
2012-01-05 10:04:15 UTC
Permalink
Hi all,

I have Jersey and JAXB used for translating json and xml to objects.
My objects define attributes and in json they go out with @ sign. In json
as far as I know there is no importance to the order of the name, value
pairs.

The question is: if we define attribute do they break the above assumption?
is this: {"event":{"state":"ended","@type":"state"}}
different from this: {"event":{"@type":"state", "state":"ended"}}
when jersey build the java object?

Thanks
John Yeary
2012-01-05 13:30:47 UTC
Permalink
JSON is a name=value pair mapping, so order is not important for that. Your
application as noted should be fine.

If you need to have order, you may consider wrapping the objects in a
GenericEntity<List<?>> to maintain some ordering. In general, the arrays
created in JSON are ordered from the Java side of the application. So using
arrays will maintain order from the server side. I can not vouch for what
your client side code will do.

John
Post by Oded Hassidi
Hi all,
I have Jersey and JAXB used for translating json and xml to objects.
as far as I know there is no importance to the order of the name, value
pairs.
The question is: if we define attribute do they break the above assumption?
when jersey build the java object?
Thanks
--
John Yeary
--
http://javaevangelist.blogspot.com
http://www.johnyeary.com
*@jyeary*

"Far better it is to dare mighty things, to win glorious triumphs, even
though checkered by failure, than to take rank with those poor spirits who
neither enjoy much nor suffer much, because they live in the gray twilight
that knows not victory nor defeat."
-- Theodore Roosevelt
Oded Hassidi
2012-01-05 19:04:26 UTC
Permalink
the issue is that my object (entity) defines type as an attribute and the
state as element and when I send the request with the attribute second the
object comes to the resource with the an empty attribute
Post by John Yeary
JSON is a name=value pair mapping, so order is not important for that.
Your application as noted should be fine.
If you need to have order, you may consider wrapping the objects in a
GenericEntity<List<?>> to maintain some ordering. In general, the arrays
created in JSON are ordered from the Java side of the application. So using
arrays will maintain order from the server side. I can not vouch for what
your client side code will do.
John
Post by Oded Hassidi
Hi all,
I have Jersey and JAXB used for translating json and xml to objects.
as far as I know there is no importance to the order of the name, value
pairs.
The question is: if we define attribute do they break the above assumption?
when jersey build the java object?
Thanks
--
John Yeary
--
http://javaevangelist.blogspot.com
http://www.johnyeary.com
"Far better it is to dare mighty things, to win glorious triumphs, even
though checkered by failure, than to take rank with those poor spirits who
neither enjoy much nor suffer much, because they live in the gray twilight
that knows not victory nor defeat."
-- Theodore Roosevelt
John Yeary
2012-01-05 19:08:32 UTC
Permalink
Have you looked at BadgerFish for encoding your JSON objects? The
JAXBContext can be configured to use BadgerFish which handles XML
attributes and values. I think that BadgerFish uses @ symbols so you may
need to change that. It does however encode both attributes and elements
correctly.
Post by Oded Hassidi
the issue is that my object (entity) defines type as an attribute and the
state as element and when I send the request with the attribute second the
object comes to the resource with the an empty attribute
Post by John Yeary
JSON is a name=value pair mapping, so order is not important for that.
Your application as noted should be fine.
If you need to have order, you may consider wrapping the objects in a
GenericEntity<List<?>> to maintain some ordering. In general, the arrays
created in JSON are ordered from the Java side of the application. So using
arrays will maintain order from the server side. I can not vouch for what
your client side code will do.
John
Post by Oded Hassidi
Hi all,
I have Jersey and JAXB used for translating json and xml to objects.
json as far as I know there is no importance to the order of the name,
value pairs.
The question is: if we define attribute do they break the above assumption?
when jersey build the java object?
Thanks
--
John Yeary
--
http://javaevangelist.blogspot.com
http://www.johnyeary.com
"Far better it is to dare mighty things, to win glorious triumphs, even
though checkered by failure, than to take rank with those poor spirits who
neither enjoy much nor suffer much, because they live in the gray twilight
that knows not victory nor defeat."
-- Theodore Roosevelt
--
John Yeary
--
http://javaevangelist.blogspot.com
http://www.johnyeary.com
*@jyeary*

"Far better it is to dare mighty things, to win glorious triumphs, even
though checkered by failure, than to take rank with those poor spirits who
neither enjoy much nor suffer much, because they live in the gray twilight
that knows not victory nor defeat."
-- Theodore Roosevelt
Tatu Saloranta
2012-01-05 19:58:02 UTC
Permalink
Post by John Yeary
Have you looked at BadgerFish for encoding your JSON objects? The
JAXBContext can be configured to use BadgerFish which handles XML
need to change that. It does however encode both attributes and elements
correctly.
Alternatively, you could simplify things by using POJO mapping -- since
JSON has no attribute/element distinction, it's much simpler to just ignore
those.
This does not affect XML side since it would still use JAXB which
differentiates between the cases, but there's really no direct connection
between XML and JSON bindings: even when using conventions like Badgerfish,
workflows are fully separate.

-+ Tatu +-
Oded Hassidi
2012-01-05 20:37:54 UTC
Permalink
Thanks for the help guys, I like the BadgerFish solution and I am using it
in the new API I'm exposing but the old already works with attributes and I
don't want to chat how client works.

Tatu - can you elaborate on your solution, and give a snippet?
Post by Tatu Saloranta
Post by John Yeary
Have you looked at BadgerFish for encoding your JSON objects? The
JAXBContext can be configured to use BadgerFish which handles XML
need to change that. It does however encode both attributes and elements
correctly.
Alternatively, you could simplify things by using POJO mapping -- since
JSON has no attribute/element distinction, it's much simpler to just ignore
those.
This does not affect XML side since it would still use JAXB which
differentiates between the cases, but there's really no direct connection
between XML and JSON bindings: even when using conventions like Badgerfish,
workflows are fully separate.
-+ Tatu +-
Tatu Saloranta
2012-01-05 21:29:03 UTC
Permalink
Post by Oded Hassidi
Thanks for the help guys, I like the BadgerFish solution and I am using it
in the new API I'm exposing but the old already works with attributes and I
don't want to chat how client works.
Tatu - can you elaborate on your solution, and give a snippet?
I'm pretty sure you can find examples with "POJO mapping jersey"
googling. It's just another provider.
Implementation left as an exercise to the reader. ;-)

-+ Tatu +-
John Yeary
2012-01-05 22:59:14 UTC
Permalink
I published an example using the mapping functionality including the code
here:

http://javaevangelist.blogspot.com/2011/12/jax-rs-tip-of-day-automatic-mapping-of.html

John
Post by Tatu Saloranta
Post by Oded Hassidi
Thanks for the help guys, I like the BadgerFish solution and I am using
it
Post by Oded Hassidi
in the new API I'm exposing but the old already works with attributes
and I
Post by Oded Hassidi
don't want to chat how client works.
Tatu - can you elaborate on your solution, and give a snippet?
I'm pretty sure you can find examples with "POJO mapping jersey"
googling. It's just another provider.
Implementation left as an exercise to the reader. ;-)
-+ Tatu +-
--
John Yeary
--
http://javaevangelist.blogspot.com
http://www.johnyeary.com
*@jyeary*

"Far better it is to dare mighty things, to win glorious triumphs, even
though checkered by failure, than to take rank with those poor spirits who
neither enjoy much nor suffer much, because they live in the gray twilight
that knows not victory nor defeat."
-- Theodore Roosevelt
John Yeary
2012-01-05 23:01:20 UTC
Permalink
I also posted an example changing the context to BadgerFish here:

http://javaevangelist.blogspot.com/2011/12/jax-rs-tip-of-day-changing-json-jaxb.html
Post by Oded Hassidi
Thanks for the help guys, I like the BadgerFish solution and I am using it
in the new API I'm exposing but the old already works with attributes and I
don't want to chat how client works.
Tatu - can you elaborate on your solution, and give a snippet?
Post by Tatu Saloranta
Post by John Yeary
Have you looked at BadgerFish for encoding your JSON objects? The
JAXBContext can be configured to use BadgerFish which handles XML
need to change that. It does however encode both attributes and elements
correctly.
Alternatively, you could simplify things by using POJO mapping -- since
JSON has no attribute/element distinction, it's much simpler to just ignore
those.
This does not affect XML side since it would still use JAXB which
differentiates between the cases, but there's really no direct connection
between XML and JSON bindings: even when using conventions like Badgerfish,
workflows are fully separate.
-+ Tatu +-
--
John Yeary
--
http://javaevangelist.blogspot.com
http://www.johnyeary.com
*@jyeary*

"Far better it is to dare mighty things, to win glorious triumphs, even
though checkered by failure, than to take rank with those poor spirits who
neither enjoy much nor suffer much, because they live in the gray twilight
that knows not victory nor defeat."
-- Theodore Roosevelt
Loading...