7.4 JSON Support

The Limax provides the complete JSON support (the Java, C#, C++ and Lua versions all provide the JSON support which is consistent with the Javascript, and maximumly ensure the usage is the same with Javascript), to communicate with the other json supported third party system.


  • 7.4.3 Relay

    Some cases need to implement this kind of system -- receive the JSON string representation from the upstream system, decode the process required by this string execution design, repackage the whole JSON component or the part of the JSON component as the data of the local system as the JSON string, and pass to the downstream system -- JSON relay system.

    • Implementation (Javascript, Lua)

      The script language generates the corresponding object hierarchy in the name space after executing the parse. If there is relay requirement, create the local object, reference or part reference the decoding result according to the name space specification of the relevant language, then execute the stringify.


    • Implementation (Java, C#, C++)

      Seen from the last line of the previous type mapping table, the input parameter of the JSON encoder supports the return type of JSON decoder, which means that the parse/stringify is reversible.

      The parse/stringify is reversible, which is the key to implement the JSON relay. The only consideration is that after executing the relative get operation on the JSON/Object or JSON/Array and the isUndefined() test of the returned JSON object is true, this returned object can not be relayed, because this object does not come from the upstream system and has no a correct representation. So the JSONException will be thrown if executing the stringify on this object.


  • 7.4.4 Exception specification

    In order to guarantee the robustness of the program, any language need to considerate the exception when executing the JSON encoding and decoding operation.

    • Encoding exception

      1. When suffering the unexplained field type, do not encode.

      2. Suffer the JSON object whose isUndefined() test is true.

      3. Object reference is a loop

      4. All kinds of runtime exception, such as the concurrent access exception and reading field failure


    • Decoding exception

      1. The syntax error of the JSON string representation


    • Decode data and catch the exception

      1. Incorrectly access the JSON component

      2. Data type conversion fail

      3. All kinds of running exception, such as the concurrent access exception


    • The consideration of the Javascript

      1. Suffer the loop when encoding, throw the exception; the type unsupported by the type mapping table is ignored, such as the function.

      2. Suffer the syntax error when decoding, throw the exception.

      3. Access the nonexistent field of the object or the accessed array is out of range when accessing the decoded result, return the undefined. Throw the exeception when executing the accessing except for the bool test and arithmetic operation on the undefined.

      4. Use the try{] catch(e){} way to catch and handle the exception.


    • The consideration of the Lua

      1. Suffer the loop when encoding, throw the exception; the type unsupported by the type mapping table is ignored, such as function and userdata.

      2. Suffer the syntax error when decoding, throw the exception.

      3. Access the nonexistent field of the object or the accessed array is out of range when accessing the decoded result, return the nil. Throw the exeception when executing the accessing except for the bool test on the nil.

      4. Use the pcall(function, args) way to catch and handle the exception.


    • The consideration of the Java and C#

      1. Suffer the loop when encoding, throw the exception; suffer the type unsupported by the type mapping table, throw the exception; suffer the JSON object whose isUndefined() test is true, throw the exception; the concurrent access exception. In particular, if the XBean supports the JSONMarshal on the server, the rule is the same as the normal accessing of XBean. The correspondent lock is necessary, and the encoding process must be completed in the transaction or the exception will be thrown.

      2. Suffer the syntax error when decoding, throw the exception

      3. Access the nonexistent field of the object or the accessed array is out of range when accessing the decoded result, return the JSON object whose isUndefined() test is true. The exception will be thrown except for the booleanValue(), isXXX() test and toString()/ToString() operation on this object. Throw the exception when calling the get(string) and keySet() on the non JSON/Object; throw the exception when the intValue(), longValue() and doubleValue() convertion fail.

      4. All kinds of exception is finally wrapped by the JSONException, so the try {} catch(JSONException e){} way should be used to catch and handle the exception.


    • The consideration of the C++

      1. The C++ does not support the encoding of the object pointed to by the pointer, so it is impossible to suffer the loop; when suffering the type unsupported by the type mapping table, the error is reported during compiling; the concurrent access does not be considerated; so the exception is thrown only when suffering the JSON object whose isUndefined() test is true.

      2. Suffer the syntax error when decoding, throw the exception.

      3. Access the nonexistent field of the object or the accessed array is out of range when accessing the decoded result, return the JSON object whose isUndefined() test is true. The exception will be thrown except for the booleanValue(), isXXX() test and toString() operation on this object. Throw the exception when calling the get(std::string) and keySet() on the non JSON/Object; throw the exception when calling the get(size_t) and toArray() on the non JSON/Array; throw the exception when the intValue(), longValue() and doubleValue() convertion fail.

      4. When the JSON operation throws the JSONException, the try{} cache(JSONException e){} way should be used to catch, the e.message could obtain the line number of the code which throws the exception to debug.


  • 7.4.5 Issues of the character set encoding

    seen from the theory, the JSON encoding and decoding process only corresponds to the input and output of the string. It is no need for JSON to consider that how these string encodes to and decodes from the byte steam in the transmission process. However, the design of the practical application is likely to consider the underlying issue.

    • The ideal case

      The sender and receiver use the compatible byte stream codec, and the both JSON codecs could obtain the correct string representation. For example, the Limax interacts with the browser via Websocket, and the both sides use the UTF8 to encode and decode, so the usage of the JSON has no any issue.


    • The better case (Java, C#)

      The java.nio.charset package of the Java, the System.Text.Encoding class of the C#, provide the complete codec. If there is special requirement, it could flexibly adapt the peer system.


    • The bad case (C++, Lua)

      Support UTF8 only,Unicode character encoded as \uHHHH can be transformed to UTF8.


Prev Next