JPJson

JPJson

Cross-platform Json parser
Version : 2.0
Price : $30
State : Out

Changelog

v2.0

  • Now compatible with ShiVa 2.0.
  • Plugin API compiled for Mac and Linux in addition to Windows : it will provide API completion in the Script module in ShiVa Editor for each of these operating systems.
  • Now uses a user token instead of an email to activate

v1.8

  • fixed a bug when parsing booleans (true was always parsed as false)

v1.7

  • When receiving json data, the status is now -3 when the data was received and the json is not well formated.

v1.6

  • Optimized for ShiVa 1.9.2

v1.5

  • Fixed an issue with slashes inside string values.

v1.4

  • JPJson is now able to parse numbers as string numbers (JPStringNumber is required). Here is the function to enable it: JPJson.useStringNumbers ( bUse )

  • Fixed a bug when parsing numbers

v1.3

  • JPJson.receive now correctly erase the previous data if a json had the same tag.

v1.2

  • Fix a parse error with numbers in a specific case.

v1.1

  • JPJson.receive is now able to take an additional parameter: sOptionalHeader

API

--JPJson API JPJson.activate ( sUserToken, sActivationKey ) --General jJson = JPJson.createFromString ( sJson, sOptTag ) jJson = JPJson.getFromTag ( sTag ) JPJson.setTag ( jJson, sTag ) sJson = JPJson.toString ( vValue, bCondensed ) JPJson.useStringNumbers ( bUse ) --Details about a Json element nSize = JPJson.getSize ( jJson ) sType = JPJson.getType ( jJson ) bYes = JPJson.isArray ( jJson ) bYes = JPJson.isMap ( jJson ) --Browse an array element: for i = 0, JPJson.getSize ( jJson ) - 1 do local vValue = jJson[i] ... end --Browse a map element: for i = 0, JPJson.getSize ( jJson ) - 1 do local sKey = jJson[i] local vValue = jJson[sKey] ... end --Receive JPJson.cancelReceiving ( sTag ) nStatus = JPJson.getReceiveStatus ( sTag ) JPJson.receive ( sTag, sURI ) --Add/remove elements JPJson.addElementInArray ( jJson, jJsonChild ) JPJson.addElementInMap ( jJson, sKey, jJsonChild ) JPJson.insertElementInArray ( jJson, nIndex, jJsonChild ) JPJson.removeElementFromArray ( jJson, nIndex ) JPJson.removeElementFromMap ( jJson, sKey )

Description

Json is an alternative to XML. It is a human-readable text format to transmit data objects consisting of attribute–value pairs. It is used primarily to transmit data to/from a server.

The Json format has several advantages over XML:

  • It is easier to read as it is not based on tags.
  • More compact than XML, resulting on less network consumption to transmit the same data.
  • Clever structure based on arrays, maps and standard types (number, boolean, string, null)

Have a look to an XML structure:

<xml>
    <name>John</name>
    <id>82</id>
    <rank>3</rank>
    <score>32410</score>
    <rank>3</rank>
    <friends_id>
        <id>119</id>
        <id>384</id>
        <id>42</id>
        <id>746</id>
    </friends_id>
    <location>
        <city>New York</city>
        <country>US</country>
    </location>
</xml>

Now, look at the equivalent Json structure, with the same data:

{ "name":"John", "id":82, "rank":3, "score":32410, "rank":3, "friends_id":[119,384,42,746], "location":{ "city":"New York", "country":"US" } }

The Json is roughly 40% lighter and easier to read because it is consisting of attribute–value pairs while XML is based on tags.

JPJson offers 2 ways to create a Json object:

  • From a Json string: jJson = JPJson.createFromString ( sJson )
  • From a Json you have downloaded: JPJson.receive ( sTag, sURI ) and jJson = JPJson.getFromTag ( sTag )

Now if you want to get the name of the player or its score, it's really easy, look:

local sName = jJson["name"] local nScore = jJson["score"]

Accessing a sub-element is also a piece of cake:

local sCountry = jJson["location"]["country"]

This was for accessing values contained in maps (or objects as it is commonly named in Json). Here is how to browse an array (defined with square brackets):

for i = 0, JPJson.getSize ( jJson["friends_id"] ) - 1 do local nFriendId = jJson["friends_id"][i] --Your code here end

It is really easy, no need to use the API for browsing the Json element, you can access it directly with keys and indexes!