|
| 1 | + |
| 2 | +[[_faq_]] |
| 3 | += FAQ |
| 4 | + |
| 5 | +== If I use a LinkedHasMap to create a JSONObject the order is not preserved, why ? |
| 6 | + |
| 7 | +The answer is in the JSON specification "An object is an unordered set of name/value pairs. |
| 8 | +An object begins with `{` (left brace) and ends with `}` (right brace). |
| 9 | +Each name is followed by `:` (colon) and the name/value pairs are separated by `,` (comma).". |
| 10 | + |
| 11 | +JSONObject uses a HashMap for its properties representation because the order of its properties is not important. |
| 12 | + |
| 13 | +<<_faq_,icon:arrow-up[] top>> |
| 14 | + |
| 15 | +== Json-lib creates empty JSONObjects from my bean class, help me! |
| 16 | + |
| 17 | +Json-lib uses the JavaBeans convention to inspect your beans and create JSONObjects. |
| 18 | +If the properties of your beans do not adhere to the convention, the resulting JSONObject will be empty or half empty. |
| 19 | +You must provide a read/write method pair for each property. |
| 20 | + |
| 21 | +<<_faq_,icon:arrow-up[] top>> |
| 22 | + |
| 23 | +== How do I configure Json-lib as a dependency with Maven2 ? |
| 24 | + |
| 25 | +[source,xml,options="nowrap"] |
| 26 | +[subs="attributes,verbatim"] |
| 27 | +---- |
| 28 | +<dependency> |
| 29 | + <groupId>{project-group}</groupId> |
| 30 | + <artifactId>{project-name}</artifactId> |
| 31 | + <version>{project-version}</version> |
| 32 | +</dependency> |
| 33 | +---- |
| 34 | + |
| 35 | +<<_faq_,icon:arrow-up[] top>> |
| 36 | + |
| 37 | +== How can I transform a JSON string into a bean with an Enum property ? |
| 38 | + |
| 39 | +You'll have to register a Morpher that can handle your Enum. Json-lib conveniently includes such a Morpher, |
| 40 | +so the only thing left to do is configure it and register the Morpher into the MorpherRegistry as follows |
| 41 | +`JSONUtils.getMorpherRegistry().registerMorpher(new EnumMorpher(MyEnum.class))` |
| 42 | + |
| 43 | +This step is optional since Json-lib 2.2 |
| 44 | + |
| 45 | +<<_faq_,icon:arrow-up[] top>> |
| 46 | + |
| 47 | +== How can I transform a simple value into a complex one when serializing back to Java ? |
| 48 | + |
| 49 | +Perhaps you've come across a scenario where you'll want a JSON string like "{'id':1}" transformed back to a Java class, |
| 50 | +but the catch is that the id property is not a simple value but another class (a complex value), say a wrapper around a |
| 51 | +primitive long. If you try to serialize such a string back to Java you'll get a ClassCastException. The solution to this |
| 52 | +problem is registering a Morpher that can handle the input and transform it into an instance of the expected type, in |
| 53 | +this case the class of the id property. |
| 54 | + |
| 55 | +<<_faq_,icon:arrow-up[] top>> |
| 56 | + |
| 57 | +== Json-lib does not find my inner bean, help! |
| 58 | + |
| 59 | +In order for Json-lib (in fact the PropertyDescriptors) to find and inspect your beans they have to be declared public, |
| 60 | + and all desired properties must have a public pair of reader/writer. The same applies to beans declared as inner classes |
| 61 | + (which by the way must also be declared static). Its a good practice to define each bean in its own file, but if you |
| 62 | + can't please make your inner beans public and static if possible. |
| 63 | + |
| 64 | +<<_faq_,icon:arrow-up[] top>> |
| 65 | + |
| 66 | +== I'd like to use XPath-like expressions with JSON, how do I do it ? |
| 67 | + |
| 68 | +Use link:http://commons.apache.org/jxpath/[JXPath], which is a simple yet powerful project that enables XPath-like |
| 69 | +expressions to be used with java beans and maps. |
| 70 | + |
| 71 | +<<_faq_,icon:arrow-up[] top>> |
0 commit comments