Package org.webpki.json


package org.webpki.json

JSON - Encoder, Decoder, Signatures, and Encryption

This package contains classes for parsing and generating messages in JSON.

To cope with browsers, this implementation is data wise compatible with EcmaScript (JavaScript) beginning with version 6.

To make the system on par with XML Schemas the JSON library also supports:
  • Registration of classes extending JSONDecoder (=supporting a specific message) and automatic instantiation during parsing. See JSONDecoderCache
  • Detection of missing reads. See JSONObjectReader.checkForUnread()
  • Strong validation of input
  • Emulation of "missing" JSON data types like True 64-bit long integer, BigInteger, BigDecimal, GregorianCalendar and byte[]

In addition, there is a set of classes supporting a [very] scaled-down JSON counterpart to enveloped XML signatures: JSON Signature Format.
See JSONObjectWriter.setSignature(JSONSigner) for programming information.

There is also a class for encrypting data using JSON formatted containers: JSON Encryption Format.
See JSONObjectWriter.createEncryptionObject(byte[],DataEncryptionAlgorithms,JSONEncrypter) for more information.

Reading and Writing JSON Data

The following examples should be sufficient for getting started with generating JSON-formated data from java.
import org.webpki.json.*;
import java.util.GregorianCalendar;

1. Writing simple objects:

JSONObjectWriter writer = new JSONObjectWriter();
writer.setInt("i", 5);
writer.setInt("j", -8).setBoolean("k", true);  // Chain commands
System.out.println(writer.toString());
Expected result:
{
  "i": 5,
  "j": -8,
  "k": true
}

2. Writing nested objects:

JSONObjectWriter writer = new JSONObjectWriter();
writer.setInt("i", 5);
writer.setInt("j", -8).setBoolean("k", true);  // Chain commands
JSONObjectWriter inner = writer.setObject("o");
inner.setString("s", "hi");
inner.setDateTime("now", new GregorianCalendar(), true);  // Date UTC-only option
System.out.println(writer.toString());
Expected result:
{
  "i": 5,
  "j": -8,
  "k": true,
  "o":
    {
      "s": "hi",
      "now": "2014-12-26T16:25:23Z"
    }
}

3. Writing arrays:

JSONArrayWriter writer = new JSONArrayWriter();
writer.setInt(5);
writer.setInt(-8).setBoolean(true);  // Chain commands
JSONObjectWriter inner = writer.setObject();
inner.setString("s", "hi").setDateTime("now", new GregorianCalendar(), true);  // Date UTC-only option
System.out.println(writer.toString());
Expected result:
[5,-8,true,
{
  "s": "hi",
  "now": "2014-12-26T16:40:28Z"
}]

4. Reading objects

String json = "{\"value\": \"hi\", \"myobj\" : {\"testme\": true}, \"list\":[1,2]}";

JSONObjectReader reader = JSONParser.parse(json);
String value = reader.getString("value");
JSONObjectReader inner = reader.getObject("myobj");
boolean testme = inner.getBoolean("testme");
JSONArrayReader array = reader.getArray("list");
int first = array.getInt();
int second = array.getInt();

System.out.println(value + ' ' + testme + ' ' + first + ' ' + second);
Expected result:
hi true 1 2

5. Reading arrays

String json = "[\"hi\", {\"testme\": true}, [1,2]]";

JSONArrayReader reader = JSONParser.parse(json).getJSONArrayReader();
String value = reader.getString();
JSONObjectReader inner = reader.getObject();
boolean testme = inner.getBoolean("testme");
JSONArrayReader array = reader.getArray();
int first = array.getInt();
int second = array.getInt();

System.out.println(value + ' ' + testme + ' ' + first + ' ' + second);
Expected result:
hi true 1 2