JSON doesn't allow trailing commas

This commit is contained in:
Richard Feldman 2022-07-17 15:05:57 -04:00
parent 31f7d72ce0
commit 4af506c530
No known key found for this signature in database
GPG key ID: 7E4127D1E4241798
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

View file

@ -490,7 +490,7 @@ fn encode_derived_record_one_field_string() {
_ -> "<bad>"
"#
),
RocStr::from(r#"{"a":"foo",}"#),
RocStr::from(r#"{"a":"foo"}"#),
RocStr
)
}
@ -513,7 +513,7 @@ fn encode_derived_record_two_fields_strings() {
_ -> "<bad>"
"#
),
RocStr::from(r#"{"a":"foo","b":"bar",}"#),
RocStr::from(r#"{"a":"foo","b":"bar"}"#),
RocStr
)
}
@ -537,7 +537,7 @@ fn encode_derived_nested_record_string() {
_ -> "<bad>"
"#
),
RocStr::from(r#"{"a":{"b":"bar",},}"#),
RocStr::from(r#"{"a":{"b":"bar"}}"#),
RocStr
)
}
@ -561,7 +561,7 @@ fn encode_derived_tag_one_payload_string() {
_ -> "<bad>"
"#
),
RocStr::from(r#"{"A":["foo",]}"#),
RocStr::from(r#"{"A":["foo"]}"#),
RocStr
)
}
@ -585,7 +585,7 @@ fn encode_derived_tag_two_payloads_string() {
_ -> "<bad>"
"#
),
RocStr::from(r#"{"A":["foo","bar",]}"#),
RocStr::from(r#"{"A":["foo","bar"]}"#),
RocStr
)
}
@ -610,7 +610,7 @@ fn encode_derived_nested_tag_string() {
_ -> "<bad>"
"#
),
RocStr::from(r#"{"A":[{"B":["foo","bar",]},]}"#),
RocStr::from(r#"{"A":[{"B":["foo","bar"]}]}"#),
RocStr
)
}
@ -635,7 +635,7 @@ fn encode_derived_nested_record_tag_record() {
_ -> "<bad>"
"#
),
RocStr::from(r#"{"a":{"B":[{"c":"foo",},]},}"#),
RocStr::from(r#"{"a":{"B":[{"c":"foo"}]}}"#),
RocStr
)
}
@ -683,7 +683,7 @@ fn encode_derived_list_of_records() {
_ -> "<bad>"
"#
),
RocStr::from(r#"[{"a":"foo",},{"a":"bar",},{"a":"baz",}]"#),
RocStr::from(r#"[{"a":"foo"},{"a":"bar"},{"a":"baz"}]"#),
RocStr
)
}