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; }
Packages | |
ceylon.json | A JSON parser / serialiser |
ceylon.json.stream | Representation of JSON data as Event pulled from an |
Dependencies | ||
ceylon.collection | 1.2.2 |
A JSON parser / serialiser
Aliases | |
ObjectValue | Source Codeshared ObjectValue=> String|Boolean|Integer|Float|Object|Array |
Value | Source Codeshared Value=> ObjectValue? |
Functions | |
parse | Source Codeshared Value parse(String str) Parses a JSON string into a JSON value Throws
By: Stéphane Épardaud |
parseFalse | Source Codeshared Boolean parseFalse(Tokenizer tokenizer) Parse false, consuming any initial whitespace |
parseKeyOrString | Source Codeshared String parseKeyOrString(Tokenizer tokenizer) Parse a String literal, consuming any initial whitespace |
parseNull | Source Codeshared Null parseNull(Tokenizer tokenizer) Parse null, consuming any initial whitespace |
parseNumber | Source Codeshared Integer|Float parseNumber(Tokenizer tokenizer) Parse a number, consuming any initial whitepsace. |
parseTrue | Source Codeshared Boolean parseTrue(Tokenizer tokenizer) Parse true, consuming any initial whitespace |
visit | Source Codeshared void visit(Value subject, Visitor visitor, Boolean sortedKeys = false) Recursively visit the given subject using the given visitor. If
Parameters:
By: Tom Bentley |
Interfaces | |
Positioned | Source Codeshared Positioned Contract for stateful iterators, tokenizers etc which have the concept of a 'current position'. |
Visitor | Source 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 |
Classes | |
Array | Source Codeshared Array Represents a JSON Array |
Builder | Source Codeshared Builder A This would usually be used in conjunction with
a |
Emitter | Source Codeshared abstract Emitter A |
Object | Source Codeshared Object Represents a JSON Object |
Parser | Source Codeshared Parser A parser for JSON data presented as a Tokenizer which calls To construct a JSON model the visitor would be a |
Printer | Source Codeshared abstract Printer A JSON Printer |
StringEmitter | Source Codeshared StringEmitter A JSON Emitter that prints to a |
StringParser | Source Codeshared StringParser A parser for JSON data presented as a String which calls To construct a JSON model the visitor would be a |
StringPrinter | Source Codeshared StringPrinter A JSON Printer that prints to a |
StringTokenizer | Source Codeshared StringTokenizer An implementation of Tokenizer using a String |
Tokenizer | Source Codeshared abstract Tokenizer Contract for a tokenizer |
Exceptions | |
InvalidTypeException | Source Codeshared InvalidTypeException |
ParseException | Source Codeshared ParseException An Exception throw during parse errors |