Contains everything required to parse and serialise JSON data.

Sample usage for parsing and accessing JSON:

import ceylon.json {
    parse, Object = Object
}

String getAuthor(String json){
    value parsedJson = parse(json);
    "author must be a string"
    assert(is Object parsedJson, is String author = parsedJson["author"]);
    return author;
}

Alternatively, this variation will result in an InvalidTypeException instead of an AssertionError if the input JSON data doesn't have the expected format:

import ceylon.json {
    parse, Object
}

String getAuthor(String json){
    assert(is Object parsedJson = parse(json));
    return parsedJson.getString("author");
}

You can iterate JSON objects too:

import ceylon.json {
    parse, Array, Object
}

{String*} getModules(String json){
    assert(is Object parsedJson = parse(json));
    if(is Array modules = parsedJson.get("modules")){
        return { for (mod in modules) 
                   if(is Object mod, 
                      is String name = mod.get("name")) 
                     name 
               };
    }
    throw Exception("Invalid JSON data");
}

Sample usage for generating JSON data:

import ceylon.json {
    Object, Array
}

String getJSON(){
    value json = Object {
        "name" -> "Introduction to Ceylon",
        "authors" -> Array {
            "Stef Epardaud",
            "Emmanuel Bernard"
        }
    };
    return json.string;
}
By: Stéphane Épardaud, Tom Bentley
License: Apache Software License
Packages
ceylon.json

A JSON parser / serialiser

ceylon.json.stream

Representation of JSON data as Event pulled from an Iterator.

Dependencies
ceylon.collection1.3.0

A JSON parser / serialiser

By: Stéphane Épardaud
Aliases
ObjectValueshared ObjectValue=> String|Boolean|Integer|Float|Object|Array

A JSON value, a String, Boolean, Integer, Float, JSON Object, or JSON Array.

This means Value except Null.

Valueshared Value=> ObjectValue?

A JSON value, a String, Boolean, Integer, Float, JSON Object, JSON Array, or Null.

Functions
parseshared Value parse(String str)

Parses a JSON string into a JSON value

Throws
  • Exception

    If the JSON string is invalid

By: Stéphane Épardaud
parseFalseshared Boolean parseFalse(Tokenizer tokenizer)

Parse false, consuming any initial whitespace

parseKeyOrStringshared String parseKeyOrString(Tokenizer tokenizer)

Parse a String literal, consuming any initial whitespace

parseNullshared Null parseNull(Tokenizer tokenizer)

Parse null, consuming any initial whitespace

parseNumbershared Integer|Float parseNumber(Tokenizer tokenizer)

Parse a number, consuming any initial whitepsace.

parseTrueshared Boolean parseTrue(Tokenizer tokenizer)

Parse true, consuming any initial whitespace

visitshared void visit(Value subject, Visitor visitor, Boolean sortedKeys = false)

Recursively visit the given subject using the given visitor. If sortedKeys is true then the keys of Objects will be visited in alphabetical order

Parameters:
  • subject

    The value to visit.

  • visitor

    The visitor to apply.

  • sortedKeys = false

    Whether keys should be visited in alphabetical order, when visiting objects.

By: Tom Bentley
Interfaces
Positionedshared Positioned

Contract for stateful iterators, tokenizers etc which have the concept of a 'current position'.

Visitorshared Visitor

Callable interface used when traversing JSON data.

It is the callers responsiblity to ensure the methods of this interface are called in a sequence that corresponds to well-formed JSON. For example, callers should never generate the calling sequence onKey(), onKey().

Classes
Arrayshared Array

Represents a JSON Array

Buildershared Builder

A Visitor that constructs a Value.

This would usually be used in conjunction with a StringParser.

Emittershared abstract Emitter

A Visitor which emits formatted JSON to the Emitter.print() method.

JsonArrayshared JsonArray

Alternative name for Array which avoids collision with ceylon.language::Array.

JsonObjectshared JsonObject

Alternative name for Object which avoids collision with ceylon.language::Object.

Objectshared Object

Represents a JSON Object

Parsershared Parser

A parser for JSON data presented as a Tokenizer which calls
the given visitor for each matched rule.

To construct a JSON model the visitor would be a Builder.

Printershared abstract Printer

A JSON Printer

StringEmittershared StringEmitter

A JSON Emitter that prints to a String.

StringParsershared StringParser

A parser for JSON data presented as a String which calls
the given visitor for each matched rule.

To construct a JSON model the visitor would be a Builder.

StringPrintershared StringPrinter

A JSON Printer that prints to a String.

StringTokenizershared StringTokenizer

An implementation of Tokenizer using a String

Tokenizershared abstract Tokenizer

Contract for a tokenizer

Exceptions
InvalidTypeExceptionshared InvalidTypeException

Represents the failure of a type conversion.
An instance is typically thrown as a result of trying to get and convert an Object member or Array element which cannot be converted to the requested or implied type.

ParseExceptionshared ParseException

An Exception throw during parse errors