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. SeeJSONDecoderCache
- 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
andbyte[]
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
-
ClassDescriptionReads JSON array elements.Writes JSON arrays.Initiator object for asymmetric key encryptions.Initiator object for asymmetric key signatures.Initiator object for asymmetric key signature verifiers.Common crypto support for JSF and JEF.For building "extensions" decodersHolds list of supported "extensions" decoders.KeyID parameter to OptionsCommon JEF/JSF decoding options.Public key parameter to OptionsBase class for java classes which can be created from specific JSON object types.Stores
JSONDecoder
classes for automatic instantiation during parsing.Holds parsed JEF (JSON Encryption Format) data.JEF (JSON Encryption Format) support.Base class for java classes which are used for creating specific JSON object types.Support class for encryption generators.Wrapper for making the WebPKI JSON library only throw unchecked exceptions.Initiator object for HMAC signatures.Initiator object for HMAC signature verifiers.JSON object reader.Creates JSON objects and performs serialization using a variety of formats.Support interface for dynamic JSON generation.JSON output formats.Parses JSON string/byte array data.Decoder for JSF signatures.Supported signature typesSupport class for signature generators.Initiator object for symmetric key encryptions.Basic JSON types read by the parser.Support class for signature verifiers.Initiator object for certificate based encryptions.Initiator object for X.509 signatures.Initiator object for X.509 signature verifiers.JSON as specified by ECMAScript