1602/json-value/3.0.0

NOTE: this package is not compatible with Elm 0.19.1

Json.Value

Definitions

= ObjectValue (List ( String, JsonValue ))
| ArrayValue (List JsonValue)
| BoolValue Bool
| NullValue
| NumericValue Float
| StringValue String

Type representing json value according to spec

Reading

getIn : List String -> JsonValue -> Result String JsonValue

Get json value at given path

ObjectValue [ ( "foo", StringValue "bar" ) ]
    |> getIn [ "foo" ]
    |> Expect.equal (Ok <| StringValue "bar")

Manipulation

setIn : List String -> JsonValue -> JsonValue -> Result String JsonValue

Set json value at given path

ObjectValue [ ( "foo", NullValue ) ]
    |> setIn [ "foo" ] (StringValue "bar")
    |> Expect.equal
        (Ok (ObjectValue [ ( "foo", StringValue "bar" ) ]))
( List String, Int )
-> String
-> JsonValue
-> Result String JsonValue

Rename property in json value

StringValue "bar"
    |> inObjWithProp "foo"
    |> setPropertyName ( [], 0 ) "bam"
    |> Expect.equal (Ok <| ObjectValue <| [ ( "bam", StringValue "bar" ) ])
deleteIn : List String -> JsonValue -> Result String JsonValue
Delete path in json object

Transforms

decoder : Decoder JsonValue

Decoder for JsonValue

[ ( "str", Json.Encode.string "value" )
, ( "array", Json.Encode.list
    [ Json.Encode.int 10
    , Json.Encode.float 0.1
    , Json.Encode.bool False
    , Json.Encode.null
    ] )
]
    |> object
    |> Json.Decode.decodeValue decoder
    |> Expect.equal
        (Ok <|
            ObjectValue
                [ ( "str", StringValue "value" )
                , ( "array", ArrayValue
                    [ NumericValue 10
                    , NumericValue 0.1
                    , BoolValue False
                    , NullValue
                    ] )
                ]
        )
encode : JsonValue -> Value

Encoder for JsonValue

[ ( "str", StringValue "value" )
, ( "array", ArrayValue
    [ NumericValue 10
    , NumericValue 0.1
    , BoolValue False
    , NullValue
    ] )
]
    |> ObjectValue
    |> encode
    |> Expect.equal
        ([ ( "str", Json.Encode.string "value" )
         , ( "array", Json.Encode.list
            [ Json.Encode.int 10
            , Json.Encode.float 0.1
            , Json.Encode.bool False
            , Json.Encode.null
            ] )
         ]
            |> object
        )

Helpers

decodeValue : Value -> JsonValue

A helper function to decode value. JsonValue decoder always succeeds, this helper aims to reduce unnecessary noise in code.