Menu

Special character not processed?!

Anonymous
2009-10-29
2013-04-29
  • Anonymous

    Anonymous - 2009-10-29

    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 **\"**, 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"><</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">'\"'</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">"\\\""</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">'\\'</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">"\\\\"</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">'/'</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">"\\/"</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">'\b'</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">"\\b"</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">'\f'</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">"\\f"</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">'\n'</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">"\\n"</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">'\r'</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">"\\r"</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">'\t'</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">"\\t"</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>

     
  • aalmiray

    aalmiray - 2009-10-29

    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

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.