Merge pull request #3564 from rtfeldman/fix-json

JSON doesn't allow trailing commas
This commit is contained in:
Ayaz 2022-07-17 21:43:01 -05:00 committed by GitHub
commit d354dd177a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 18 deletions

View file

@ -80,18 +80,18 @@ string = \s -> custom \bytes, @Json {} ->
list = \lst, encodeElem ->
custom \bytes, @Json {} ->
writeList = \{ buffer, elemIndex }, elem ->
bufferWithPrefix =
if elemIndex > 0 then
List.append buffer (Num.toU8 ',')
writeList = \{ buffer, elemsLeft }, elem ->
bufferWithElem = appendWith buffer (encodeElem elem) (@Json {})
bufferWithSuffix =
if elemsLeft > 1 then
List.append bufferWithElem (Num.toU8 ',')
else
buffer
bufferWithElem = appendWith bufferWithPrefix (encodeElem elem) (@Json {})
bufferWithElem
{ buffer: bufferWithElem, elemIndex: elemIndex + 1 }
{ buffer: bufferWithSuffix, elemsLeft: elemsLeft - 1 }
head = List.append bytes (Num.toU8 '[')
{ buffer: withList } = List.walk lst { buffer: head, elemIndex: 0 } writeList
{ buffer: withList } = List.walk lst { buffer: head, elemsLeft: List.len lst } writeList
List.append withList (Num.toU8 ']')
@ -106,7 +106,7 @@ record = \fields ->
|> appendWith value (@Json {})
bufferWithSuffix =
if fieldsLeft > 0 then
if fieldsLeft > 1 then
List.append bufferWithKeyValue (Num.toU8 ',')
else
bufferWithKeyValue
@ -124,7 +124,7 @@ tag = \name, payload ->
writePayload = \{ buffer, itemsLeft }, encoder ->
bufferWithValue = appendWith buffer encoder (@Json {})
bufferWithSuffix =
if itemsLeft > 0 then
if itemsLeft > 1 then
List.append bufferWithValue (Num.toU8 ',')
else
bufferWithValue