github.com/j8r/con
0.2.2 / published Nov 9, 2019 / repository
A simple, fast and readable JSON-compatible serialization format
CON
Cretin Object Notation - a simple, fast and readable JSON-compatible serialization format
The specifications is mostly stable, but can be subject to minor changes.
Example
key "string"
pi 3.14
hash {
enable true
nothing nil
}
ports [
22
1234
8888
]
Features
- Backward compatible with JSON
- Easy to read, fast to parse
- Simple specifications
Documentation
https://j8r.github.io/con
Specifications
Specification document: SPEC.md
Usage
Add the dependency to your shard.yml:
dependencies:
con:
github: j8r/con
This object is a convenient container.
A CON::Any can be converted back to either a CON or JSON serialized String
require "con"
con_any = CON.parse con_data
con_any.to_con
require "json"
con_any.to_json
CON::PullParser can be used to parse more efficiently, if the mapping is known in advance.
See spec for more test examples.
Benchmarks
There are benchmarks comparing CON and the stdlib's JSON implementation
crystal run --release benchmark/*
Some results:
CON.parse minified 527.25k ( 1.90µs) (±13.77%) 1.88kB/op fastest
CON.parse pretty 501.42k ( 1.99µs) (±15.08%) 1.88kB/op 1.05× slower
JSON.parse minified 487.52k ( 2.05µs) (± 9.05%) 2.08kB/op 1.08× slower
JSON.parse pretty 409.80k ( 2.44µs) (± 9.13%) 2.08kB/op 1.29× slower
#to_con 1.17M (852.99ns) (± 3.51%) 320B/op fastest
#to_pretty_con 1.10M (908.96ns) (± 5.76%) 320B/op 1.07× slower
#to_json 742.97k ( 1.35µs) (±14.52%) 576B/op 1.58× slower
#to_pretty_json 612.21k ( 1.63µs) (±13.04%) 848B/op 1.91× slower
License
Copyright (c) 2018-2019 Julien Reichardt - ISC License
API
- Array(T)
An
Arrayis an ordered, integer-indexed collection of objects of type T. - BigDecimal
A
BigDecimalcan represent arbitrarily large precision decimals. - BigFloat
A
BigFloatcan represent arbitrarily large floats. - BigInt
A
BigIntcan represent arbitrarily large integers. - Bool
Bool has only two possible values:
trueandfalse. - CON
- Enum
Enum is the base type of all enums.
- Float
Float is the base type of all floating point numbers.
- Hash(K, V)
A
Hashrepresents a collection of key-value mappings, similar to a dictionary. - Int
Int is the base type of all integer types.
- JSON
- NamedTuple(**T)
A named tuple is a fixed-size, immutable, stack-allocated mapping of a fixed set of keys to values.
- Nil
The
Niltype has only one possible value:nil. - Object
Objectis the base type of all Crystal objects. - Reference
Referenceis the base class of classes you define in your program. - Regex
A
Regexrepresents a regular expression, a pattern that describes the contents of strings. - Set(T)
Setimplements a collection of values with no duplicates. - String
A
Stringrepresents an immutable sequence of UTF-8 characters. - Symbol
A symbol is a constant that is identified by a name without you having to give it a numeric value.
- Time
See https://github.com/crystal-lang/crystal/blob/master/src/json/to_json.cr
- Tuple(*T)
A tuple is a fixed-size, immutable, stack-allocated sequence of values of possibly different types.
- UUID
Adds CON support to
UUIDfor use in a CON mapping. - Union(*T)
A union type represents the possibility of a variable or an expression having more than one possible type at compile time.
- Value
Valueis the base type of the primitive types (Nil,Bool,Char,Number),Symbol,Pointer,Tuple,StaticArrayand all structs.