i'm using JSON lib 2.23, it's a pretty good lib, but sometimes pretty bad for JSON output, e.g., dealing with a little bit complicated data, like :<pre>
List<Node> nodeList;
JSONObject json=new JSONObject();
json.element("subnode",nodeList);
String jsonText=json.toString();
</pre>, and then output text are full of **\r\n** and **\&quot;**, while i want to output some fragments of XML nodes, but the output is frustrating.
Json-lib's output depends on what you put in first. However are you aware that you can register Value & Bean processors to "massage" your data at any end of the serialization process? if the default behavior is not suiting your needs you can certainly use Value and Bean processors to get what you want.
Still, if those facilities prove to be inadequate then please file a bug request and we'll process it. Thank you!
- Andres
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
i'm using JSON lib 2.23, it's a pretty good lib, but sometimes pretty bad for JSON output, e.g., dealing with a little bit complicated data, like :<pre>
List<Node> nodeList;
JSONObject json=new JSONObject();
json.element("subnode",nodeList);
String jsonText=json.toString();
</pre>, and then output text are full of **\r\n** and **\&quot;**, while i want to output some fragments of XML nodes, but the output is frustrating.
so i have to replace all these "special characters" using reg exp, can this JSON output functionality be improved very soon? I think it's not that hard, just call a static method to transform very JSON element into right format of text, something like this:
<DIV style="BACKGROUND: #fdfdfd; COLOR: black"><U>incomplete source</U>: </DIV>
<DIV style="FONT-FAMILY: '', 'Consolas', 'Lucida Console', 'Courier New'; COLOR: #000000" class=source jQuery1256856672937="14"><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">public</SPAN> <SPAN style="COLOR: #000080; FONT-WEIGHT: bold">static</SPAN> <SPAN style="COLOR: #000000">toJsonString</SPAN>(<SPAN style="COLOR: #000000">String</SPAN> s<SPAN style="COLOR: #000000">){</SPAN><BR><SPAN style="COLOR: #000000">……</SPAN><BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">for</SPAN>(<SPAN style="COLOR: #000000">inti</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #0000ff">0</SPAN>;<SPAN style="COLOR: #000000">i</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN>s<SPAN style="COLOR: #000000">.</SPAN><SPAN style="COLOR: #ff0000">length</SPAN>();<SPAN style="COLOR: #000000">i</SPAN><SPAN style="COLOR: #000000">++){</SPAN><BR><SPAN style="COLOR: #000000">charc</SPAN><SPAN style="COLOR: #000000">=</SPAN>s<SPAN style="COLOR: #000000">.</SPAN><SPAN style="COLOR: #ff0000">charAt</SPAN>(<SPAN style="COLOR: #000000">i</SPAN>);<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">switch</SPAN>(<SPAN style="COLOR: #000000">c</SPAN><SPAN style="COLOR: #000000">){</SPAN><BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">case</SPAN><SPAN style="COLOR: #800080">&#39;\&amp;quot;&#39;</SPAN><SPAN style="COLOR: #000000">:</SPAN><BR><SPAN style="COLOR: #000000">buffer</SPAN><SPAN style="COLOR: #000000">.</SPAN><SPAN style="COLOR: #ff0000">append</SPAN>(<SPAN style="COLOR: #0000ff">&quot;\\\&amp;quot;"</SPAN>);<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">break</SPAN>;<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">case</SPAN><SPAN style="COLOR: #800080">&#39;\\&amp;#39;</SPAN><SPAN style="COLOR: #000000">:</SPAN><BR><SPAN style="COLOR: #000000">buffer</SPAN><SPAN style="COLOR: #000000">.</SPAN><SPAN style="COLOR: #ff0000">append</SPAN>(<SPAN style="COLOR: #0000ff">&quot;\\\\&amp;quot;</SPAN>);<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">break</SPAN>;<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">case</SPAN><SPAN style="COLOR: #800080">&#39;/&#39;</SPAN><SPAN style="COLOR: #000000">:</SPAN><BR><SPAN style="COLOR: #000000">buffer</SPAN><SPAN style="COLOR: #000000">.</SPAN><SPAN style="COLOR: #ff0000">append</SPAN>(<SPAN style="COLOR: #0000ff">&quot;\\/&quot;</SPAN>);<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">break</SPAN>;<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">case</SPAN><SPAN style="COLOR: #800080">&#39;\b&#39;</SPAN><SPAN style="COLOR: #000000">:</SPAN><BR><SPAN style="COLOR: #000000">buffer</SPAN><SPAN style="COLOR: #000000">.</SPAN><SPAN style="COLOR: #ff0000">append</SPAN>(<SPAN style="COLOR: #0000ff">&quot;\\b&quot;</SPAN>);<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">break</SPAN>;<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">case</SPAN><SPAN style="COLOR: #800080">&#39;\f&#39;</SPAN><SPAN style="COLOR: #000000">:</SPAN><BR><SPAN style="COLOR: #000000">buffer</SPAN><SPAN style="COLOR: #000000">.</SPAN><SPAN style="COLOR: #ff0000">append</SPAN>(<SPAN style="COLOR: #0000ff">&quot;\\f&quot;</SPAN>);<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">break</SPAN>;<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">case</SPAN><SPAN style="COLOR: #800080">&#39;\n&#39;</SPAN><SPAN style="COLOR: #000000">:</SPAN><BR><SPAN style="COLOR: #000000">buffer</SPAN><SPAN style="COLOR: #000000">.</SPAN><SPAN style="COLOR: #ff0000">append</SPAN>(<SPAN style="COLOR: #0000ff">&quot;\\n&quot;</SPAN>);<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">break</SPAN>;<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">case</SPAN><SPAN style="COLOR: #800080">&#39;\r&#39;</SPAN><SPAN style="COLOR: #000000">:</SPAN><BR><SPAN style="COLOR: #000000">buffer</SPAN><SPAN style="COLOR: #000000">.</SPAN><SPAN style="COLOR: #ff0000">append</SPAN>(<SPAN style="COLOR: #0000ff">&quot;\\r&quot;</SPAN>);<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">break</SPAN>;<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">case</SPAN><SPAN style="COLOR: #800080">&#39;\t&#39;</SPAN><SPAN style="COLOR: #000000">:</SPAN><BR><SPAN style="COLOR: #000000">buffer</SPAN><SPAN style="COLOR: #000000">.</SPAN><SPAN style="COLOR: #ff0000">append</SPAN>(<SPAN style="COLOR: #0000ff">&quot;\\t&quot;</SPAN>);<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">break</SPAN>;<BR><SPAN style="COLOR: #000080; FONT-WEIGHT: bold">default</SPAN><SPAN style="COLOR: #000000">:</SPAN><BR><SPAN style="COLOR: #000000">buffer</SPAN><SPAN style="COLOR: #000000">.</SPAN><SPAN style="COLOR: #ff0000">append</SPAN>(<SPAN style="COLOR: #000000">c</SPAN>);<BR><SPAN style="COLOR: #000000">}</SPAN><BR><SPAN style="COLOR: #000000">}</SPAN><BR><SPAN style="COLOR: #000000">……</SPAN><BR><SPAN style="COLOR: #000000">}</SPAN><BR></DIV>
Json-lib's output depends on what you put in first. However are you aware that you can register Value & Bean processors to "massage" your data at any end of the serialization process? if the default behavior is not suiting your needs you can certainly use Value and Bean processors to get what you want.
Still, if those facilities prove to be inadequate then please file a bug request and we'll process it. Thank you!
- Andres