Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

139 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Extensible Data Soup

An EDN library for Kotlin.

Standard things implemented

☑ Can read from strings or from input streams.
☑ Can write to strings or writers.

☑ Use UTF-8
☑ String literals
☑ Symbol
☑ Keyword
☑ Char literals
☑ Unicode Char literals (\uXXXX and \oXXX)
☑ Comments
nil, true, false

☑ Number literals

  • Signed values.
  • N and M suffixes.
  • kleinert.edn.data.Ratio type.

☑ Symbolic values ##NaN, ##INF and ##-INF
☑ Built-in tagged elements #uuid and #inst
☑ Discard with #_
☑ Tagged elements.

☑ Lists and vectors
☑ Sets
☑ Maps

☑ Metadata (optional)

Not implemented

☐ The pretty printer isn't printing so pretty yet.

@, ', ~ and backtick prefixes, # prefix for lists. (I don't see a use for them in this context.)

☐ Add optional support for explicit prefixes. (Eg. decimal 10r255, binary 2r11111111, octal 8r377, hex 16rFF)

Examples

println(EDN.read("symbol")) // Symbol without namespace
println(EDN.read("namespace/symbol")) // Symbol
println(EDN.read(":keyword")) // Keyword without namespace
println(EDN.read(":namespace/symbol")) // Keyword
println(EDN.read("\"string\"")) // String
println(EDN.read("\\c")) // Character

println(EDN.read("(list elements)")) // List
println(EDN.read("[vector elements]")) // Vector
println(EDN.read("#{set elements}")) // Set
println(EDN.read("{map-key map-value}")) // Map

println(EDN.read("12648430")) // The same Long
println(EDN.read("0xC0FFEE", EDN.defaultOptions.copy(moreNumberPrefixes = true))) // Long, hex prefix requires additional setting
println(EDN.read("12648430N")) // The same as BigInt
println(EDN.read("5.0")) // Double
println(EDN.read("5.0M")) // BigDecimal
println(EDN.read("5/6")) // Ratio

println(EDN.read("nil")) // Null
println(EDN.read("false")) // False
println(EDN.read("true")) // True

println(EDN.read("#uuid \"f81d4fae-7dec-11d0-a765-00a0c91e6bf6\"")) // UUID
println(EDN.read("#inst \"1985-04-12T23:20:50.52Z\"")) // Instant

println(EDN.read("^:a abc", EDN.defaultOptions.copy(allowMetadata = true))) // Meta
EDN.pprintln(Symbol.symbol("symbol"))                       // symbol
EDN.pprintln(Symbol.symbol("namespace/symbol"))             // namespace/symbol
EDN.pprintln(Keyword["keyword"])                            // :keyword
EDN.pprintln(Keyword["namespace/keyword"])                  // :namespace/keyword

EDN.pprintln("string")                                      // "string"
EDN.pprintln('c')                                           // \c
EDN.pprintln('')                                           // \u2626

// Without special options, Char32 is printed as String.
EDN.pprintln(Char32(0x0001F381))                            // "🎁"

// PersistentLists, Sequences and other Iterables (which are not Lists, Sets or Maps) are printed with ().
EDN.pprintln(PersistentList.of(1, 2, 3))                    // (1, 2, 3)
EDN.pprintln(sequenceOf(1, 2, 3))                           // (1, 2, 3)
EDN.pprintln(1..3)                                          // (1, 2, 3)

// Vectors, Arrays and Lists (other than PersistentList) are all printed with [].
EDN.pprintln(PersistentVector.of(1, 2, 3))                  // [1, 2, 3]
EDN.pprintln(listOf(1, 2, 3))                               // [1, 2, 3]
EDN.pprintln(intArrayOf(1, 2, 3))                           // [1, 2, 3]

// PersistentSet and other Sets are treated the same
EDN.pprintln(PersistentSet.of(1, 2, 3))                     // #{1, 2, 3}
EDN.pprintln(setOf(1, 2, 3))                                // #{1, 2, 3}

// PersistentMap and other Maps are treated the same
EDN.pprintln(PersistentMap.of(1 to 2, 3 to 4))              // {1 2, 3 4}
EDN.pprintln(mapOf(1 to 2, 3 to 4))                         // {1 2, 3 4}

EDN.pprintln(0xC0FFEE)                                      // 12648430
EDN.pprintln(12648430)                                      // 12648430
EDN.pprintln(BigInteger.valueOf(12648430))                  // 12648430N
EDN.pprintln(5.0)                                           // 5.0
EDN.pprintln(BigDecimal.valueOf(5.0))                       // 5.0M
EDN.pprintln(Ratio.valueOf(5, 6))                           // 5/6

EDN.pprintln(null)                                          // nil
EDN.pprintln(true)                                          // true
EDN.pprintln(false)                                         // false

EDN.pprintln(UUID.fromString("f81d4fae-7dec-11d0-a765-00a0c91e6bf6")) // #uuid "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"
EDN.pprintln(Instant.parse("1985-04-12T23:20:50.52Z"))      // #inst "1985-04-12T23:20:50.520Z"

EDN.pprintln(Double.POSITIVE_INFINITY)                      // ##INF
EDN.pprintln(Double.NEGATIVE_INFINITY)                      // ##-INF
EDN.pprintln(Double.NaN)                                    // ##NaN

EDN.pprintln(IObj.valueOf(mapOf(12 to 13), 159))            // ^{12 13} 159

Non-standard

I included some non-standard features. All of these can be turned off (see examples).

Option Description Default
allowSchemeUTF32Codes Allow scheme UTF-32 char codes (\\xXXXXXXXX). false
allowDispatchChars Allow dispatch UTF-32chars (#\uXXXXXXXX). false
allowNumericSuffixes Enable _i8, _16, _i32, _64, and L suffixes for integral numbers. false
allowMoreEncoderDecoderNames Allow symbols without explicit namespaces for tagged elements. false
allowUTFSymbols Allow UTF-8 strings as symbols. false
decodingSequenceSeparator Separator between elements in lists, vectors, maps, and sets. ", "
ednClassDecoders Functions for decoding tagged elements to objects. mapOf()
ednClassEncoders Functions for encoding objects to tagged elements. listOf()
listToEdnListConverter Converter function from Kotlin's default List to a list type. Implementation detail.
listToEdnVectorConverter Converter function from Kotlin's default List to a vector type. Implementation detail.
listToEdnMapConverter Converter function from Kotlin's List<Pair<*, *>> to a map type. Implementation detail.
listToEdnSetConverter Converter function from Kotlin's List (assumed to be distinct) to a set type. Implementation detail.
sequenceElementLimit Limit of elements after which lazy sequences are shortened. 10000

0x, 0o and 0b prefixes for numbers (for hexadecimal, octal and binary respectively). (Option: moreNumberPrefixes)

☑ Number suffixes for different sizes. (Option: allowNumericSuffixes)

  • _i8 (Byte)
  • _i16 (Short)
  • _i32 (Int)
  • _i64 and L (Long)

☑ Scheme-style char literals. These return strings, not chars. (Option: allowDispatchChars)

  • #\oXXX
  • #\uXXXX and #\uXXXXXXXX
  • #\xXXXXXXXX

☑ Tags can be more free in their naming. (Option: allowMoreEncoderDecoderNames)

☑ ☐

About

An EDN library for Kotlin.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages