Source Code

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.3

A JSON parser / serialiser

By: Stéphane Épardaud
Aliases
ObjectValueSource Codeshared 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.

ValueSource Codeshared Value=> ObjectValue?

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

Functions
parseSource Codeshared Value parse(String str)

Parses a JSON string into a JSON value

Throws
By: Stéphane Épardaud
parseFalseSource Codeshared Boolean parseFalse(Tokenizer tokenizer)

Parse false, consuming any initial whitespace

parseKeyOrStringSource Codeshared String parseKeyOrString(Tokenizer tokenizer)

Parse a String literal, consuming any initial whitespace

parseNullSource Codeshared Null parseNull(Tokenizer tokenizer)

Parse null, consuming any initial whitespace

parseNumberSource Codeshared Integer|Float parseNumber(Tokenizer tokenizer)

Parse a number, consuming any initial whitespace.

parseTrueSource Codeshared Boolean parseTrue(Tokenizer tokenizer)

Parse true, consuming any initial whitespace

visitSource Codeshared 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
PositionedSource Codeshared Positioned

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

VisitorSource Codeshared 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
ArraySource Codeshared Array

Represents a JSON Array

BuilderSource Codeshared Builder

A Visitor that constructs a Value.

This would usually be used in conjunction with a StringParser.

EmitterSource Codeshared abstract Emitter

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

JsonArraySource Codeshared JsonArray

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

JsonObjectSource Codeshared JsonObject

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

ObjectSource Codeshared Object

Represents a JSON Object

ParserSource Codeshared 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.

PrinterSource Codeshared abstract Printer

A JSON Printer

StringEmitterSource Codeshared StringEmitter

A JSON Emitter that prints to a String.

StringParserSource Codeshared 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.

StringPrinterSource Codeshared StringPrinter

A JSON Printer that prints to a String.

StringTokenizerSource Codeshared StringTokenizer

An implementation of Tokenizer using a String

TokenizerSource Codeshared abstract Tokenizer

Contract for a tokenizer

Exceptions
InvalidTypeExceptionSource Codeshared 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.

ParseExceptionSource Codeshared ParseException

An Exception throw during parse errors