roc format

This commit is contained in:
Luke Boswell 2023-03-15 18:54:46 +11:00
parent 2241b173bf
commit dd0fdd5d74
No known key found for this signature in database
GPG key ID: F6DB3C9DB47377B0
2 changed files with 19 additions and 17 deletions

View file

@ -1,33 +1,33 @@
## JSON is a data format that is easy for humans to read and write. It is ## JSON is a data format that is easy for humans to read and write. It is
## commonly used to exhange data between two systems such as a server and a ## commonly used to exhange data between two systems such as a server and a
## client (e.g. web browser). ## client (e.g. web browser).
## ##
## This module implements functionality to serialise and de-serialise Roc types ## This module implements functionality to serialise and de-serialise Roc types
## to and from JSON data. Using the `Encode` and `Decode` builtins this process ## to and from JSON data. Using the `Encode` and `Decode` builtins this process
## can be achieved without the need to write custom encoder and decoder functions ## can be achieved without the need to write custom encoder and decoder functions
## to parse UTF-8 strings. ## to parse UTF-8 strings.
## ##
## Here is a basic example which shows how to parse a JSON record into a Roc ## Here is a basic example which shows how to parse a JSON record into a Roc
## type named `Language` which includes a `name` field. The JSON string is ## type named `Language` which includes a `name` field. The JSON string is
## decoded and then the field is encoded back into a UTF-8 string. ## decoded and then the field is encoded back into a UTF-8 string.
## ##
## ``` ## ```
## Language : { ## Language : {
## name : Str, ## name : Str,
## } ## }
## ##
## jsonStr = Str.toUtf8 "{\"name\":\"Röc Lang\"}" ## jsonStr = Str.toUtf8 "{\"name\":\"Röc Lang\"}"
## ##
## result : Result Language _ ## result : Result Language _
## result = ## result =
## jsonStr ## jsonStr
## |> Decode.fromBytes fromUtf8 # returns `Ok {name : "Röc Lang"}` ## |> Decode.fromBytes fromUtf8 # returns `Ok {name : "Röc Lang"}`
## ##
## name = ## name =
## decodedValue <- Result.map result ## decodedValue <- Result.map result
## ##
## Encode.toBytes decodedValue.name toUtf8 ## Encode.toBytes decodedValue.name toUtf8
## ##
## expect name == Ok (Str.toUtf8 "\"Röc Lang\"") ## expect name == Ok (Str.toUtf8 "\"Röc Lang\"")
## ``` ## ```
## ##
@ -75,7 +75,7 @@ interface Json
Result, Result,
] ]
## An opaque type with the `EncoderFormatting` and ## An opaque type with the `EncoderFormatting` and
## `DecoderFormatting` abilities. ## `DecoderFormatting` abilities.
Json := {} has [ Json := {} has [
EncoderFormatting { EncoderFormatting {
@ -441,13 +441,15 @@ decodeList = \decodeElem -> Decode.custom \bytes, @Json {} ->
['[', ']'] -> { result: Ok [], rest: List.drop bytes 2 } ['[', ']'] -> { result: Ok [], rest: List.drop bytes 2 }
['[', ..] -> ['[', ..] ->
bytesWithoutWhitespace = eatWhitespace (List.dropFirst bytes) bytesWithoutWhitespace = eatWhitespace (List.dropFirst bytes)
when bytesWithoutWhitespace is when bytesWithoutWhitespace is
[']', ..] -> [']', ..] ->
{ result: Ok [], rest: List.dropFirst bytesWithoutWhitespace } { result: Ok [], rest: List.dropFirst bytesWithoutWhitespace }
_ ->
_ ->
when decodeElems bytesWithoutWhitespace [] is when decodeElems bytesWithoutWhitespace [] is
Errored e rest -> Errored e rest ->
{ result: Err e, rest } { result: Err e, rest }
Done vals rest -> Done vals rest ->
when rest is when rest is
[']', ..] -> { result: Ok vals, rest: List.dropFirst rest } [']', ..] -> { result: Ok vals, rest: List.dropFirst rest }

View file

@ -951,8 +951,8 @@ pow : Frac a, Frac a -> Frac a
## ##
## Num.exp 5 6 ## Num.exp 5 6
## ``` ## ```
## ## Performance Details ## ## Performance Details
## ##
## Be careful! It is very easy for this function to produce an answer ## Be careful! It is very easy for this function to produce an answer
## so large it causes an overflow. ## so large it causes an overflow.
powInt : Int a, Int a -> Int a powInt : Int a, Int a -> Int a