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.2 Decoding

    Javascript JSON.parse(string)
    Java JSON JSON.parse(String s);
    C# JSON JSON.parse(string s);
    C++ std::shared_ptr<JSON> JSON::parse(std::string s);
    Lua JSON.parse(string)

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

      1. The isNull(), isBoolean(), isNumber(), isString(), isArray() and isObject() methods could be used on the JSON object to test the JSON component type.

      2. The isObject() returns true, which means that the JSON object describes the JSON/Object and allows the below methods to be executed on this object, or throw the JSONException.

      Java JSON get(String key);
      C# JSON get(string key);
      C++ std::shared_ptr<JSON> get(const std::string& key) const;

      The above methods use the key as the field name to query JSON/Object and return the JSON representation of the relative field. If the key corresponding to the current JSON/Object does not exist, a JSON object is also returned. And calling the isUndefined() method on this object returns true.

      Java Set<String> keySet();
      C# ICollection<string> keySet();
      C++ std::vector<std::string> keySet() const;

      The above methods return all the field's name of the JSON/Object.


      3. The method isArrary() returns true, which means that the JSON object describes the JSON/Array and allows the below methods to be executed on this object, or throw the JSONException.

      Java JSON get(int index);
      C# JSON get(int index);
      C++ std::shared_ptr<JSON> get(size_t index) const;

      The above methods query JSON/Array with the index and return the JSON representation of the array element. If the index is out of range, a JSON object is also returned. And calling the isUndefined() method on this object returns true.

      Java JSON[] toArray();
      C# JSON[] ToArray();
      C++ std::vector<std::shared_ptr<JSON>> toArray() const;

      The above methods return the JSON object array constructed by all the elements of the JSON/Array.


      4. The booleanValue() method is consistent with the Javascript.

      If the description of the JSON object is the JSON/true, the ture is returned.

      If the description of the JSON object is the JSON/false, the false is returned.

      If the description of the JSON object is the JSON/null or the test of the isUndefined() is ture, the false is returned.

      If the description of the JSON object is the JSON/Number and the value is 0, the false is returned.

      If the description of the JSON object is the JSON/String and the length is 0, the false is returned.

      The other case returns the true.


      5. the intValue(), longValue() and doubleValue() methods

      If the description of the JSON object is the JSON/Number, the correspondent type conversion is executed and then returns.

      If the description of the JSON object is the JSON/String and this string could be converted as the target type (no digital format error), returns after the conversion.

      The other cases throw the JSONException.


      6. the toString()/ToString() method

      If the description of the JSON object is the JSON/Stirng, the correspondent string is returned.

      If the description of the JSON object is the JSON/Number, the converted numeric string is returned.

      If the description of the JSON object is the JSON/true, the ture represented by the string is returned.

      If the description of the JSON object is the JSON/false, the false represented by the string is returned.

      If the description of the JSON object is the JSON/null, the null represented by the string is returned.

      If the isUndefined() test of the JSON object is true, the undefined represented by the string is returned.

      For the other cases, the different language has the different return value and is related to the respective toString/ToString.


    • For example

      If there is JSON object represented by the string {"x":[10,"abc", 2], "y": null, "z": true}, the below implementation could be used to return the first element of the x field.

      Javascript JSON.parse("{\"x\":[10,\"abc\", 2], \"y\":null,\"z\":true}").x[0]
      Java JSON.parse("{\"x\":[10,\"abc\", 2], \"y\":null, \"z\": true}").get("x").get(0).intValue();
      C# JSON.parse("{\"x\":[10,\"abc\", 2], \"y\":null, \"z\": true}").get("x").get(0).intValue();
      C++ JSON.parse("{\"x\":[10,\"abc\", 2], \"y\":null,\"z\":true}")->get("x")->get(0)->intValue();
      Lua JSON.parse("{\"x\":[10,\"abc\", 2], \"y\":null,\"z\":true}").x[1]

      Compared with the Javascript version, the implementation of the other vesions is almost completely consistent if the language difference does not be considered.

      The most cases just simply use the known format JSON component description like the above example, and directly write without more test.


Prev Next