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
License: Apache Software License
Packages
ceylon.json

A JSON parser / serialiser

Dependencies
ceylon.collection1.1.0

A JSON parser / serialiser

By: Stéphane Épardaud
Aliases
ValueSource Codeshared Value=> String|Boolean|Integer|Float|Object|Array|NullInstance

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

Values
nilSource Codeshared nil nil

The singleton that represents the null value in JSON`.

Functions
parseSource Codeshared Value parse(String str)

Parses a JSON string into a JSON value

Throws
By: Stéphane Épardaud
Classes
ArraySource Codeshared Array

Represents a JSON Array

NullInstanceSource Codeshared abstract NullInstance

Represents the type of the null value in JSON.

ObjectSource Codeshared Object

Represents a JSON Object

PrinterSource Codeshared abstract Printer

A JSON Printer

StringPrinterSource Codeshared StringPrinter

A JSON Printer that prints to a String.

nilSource Codeshared nil

The singleton that represents the null value in JSON`.

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