allenap/elm-json-decode-broken/1.2.2

NOTE: the latest version is 3.0.2

Json.Decode.Broken

Decode broken JSON.

Parsing

parse : String -> Result (List DeadEnd) Value

Parse the given JSON string.

Errors come straight from elm/parser and may not be super useful. It may be worth changing this parser to use elm/parser's Parser.Advanced which allows more control over errors.

type Value
= Object (List ( String, Value ))
| Array (List Value)
| String String
| Number Int Frac Exp
| True
| False
| Null

Custom type for JSON values.

Mostly this is self-explanatory, but numbers might be interesting because no effort is made to convert them to Float values. This means you can deal with overflows or precision mismatches in broken JSON.

Parser building blocks

A parser for a JSON value is:

Parser.oneOf [ object, array, string, number, true, false, null ]

According to the specification, a JSON document is optional whitespace, a JSON value (that oneOf … expression above), then more optional whitespace – and that's what the json parser does. Hence parsing a compliant JSON document is nothing more than:

Parser.run json "…"

Use these building blocks to compose a parser for broken JSON as you need. If you need to parse non-compliant quoted strings, for example, it might be best to copy just the string code from this module into your project, and use the other parsers in this module – object, array, and so on – to compose a new parser.

json : Parser Value

Parser for JSON.

This is a JSON value surrounded by optional whitespace.

object : Parser Value

Parser for a JSON object.

array : Parser Value

Parser for a JSON array.

string : Parser Value

Parser for a quoted JSON string.

string and some of its helpers have been adapted from elm/parser's DoubleQuoteString example.

number : Parser Value

Parser for a JSON number.

true : Parser Value

Parser for a JSON 'true' literal.

false : Parser Value

Parser for a JSON 'false' literal.

null : Parser Value

Parser for a JSON 'null' literal.

ws : Parser ()

Parser for JSON whitespace.

This is the whitespace that appears between significant elements of JSON, and before and after JSON documents, not whitespace within quoted strings.

Numbers

type Frac
= Frac Int
| NoFrac

Does the number have a fractional component?

i.e. the optional part after the decimal point.

type Exp
= Exp Sign Int
| NoExp

Does the number have an exponent?

i.e. an optional suffix starting with e or E in the number.

type Sign
= Plus
| Minus
| NoSign

The sign of the exponent.

toFloat : Int -> Frac -> Exp -> Float

Convert a Number to a Float.

Further processing

Value is a bit unwieldy. Instead of trying to provide a mechanism here for further processing of Value, instead there's a single exit: encode. This will return you to the familiar world of elm/json.

encode : Value -> Value

Encode a Value into a Json.Encode.Value.

This can then be used with Json.Decode.decodeValue to populate data structures in a more familiar way.

Note that this converts Number with fractional parts or exponents to Float. If that conversion fails, it encodes as NaN.