Skip to content

Commit 87c4804

Browse files
author
Andy C
committed
[builtin/json] Support type_errors=false
Use it in the Hay example!
1 parent 22faf63 commit 87c4804

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

data_lang/j8.py

+5
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,9 @@ def Print(self, val, level=0):
577577

578578
if self.options & NON_DATA_IS_ERROR:
579579
raise error.Encode("Can't encode value of type Obj")
580+
elif self.options & NON_DATA_IS_NULL:
581+
self.buf.write('null')
582+
return
580583

581584
# Cycle detection, only for containers that can be in cycles
582585
heap_id = HeapValueId(val)
@@ -612,6 +615,8 @@ def Print(self, val, level=0):
612615
if self.options & NON_DATA_IS_ERROR:
613616
raise error.Encode("Can't serialize object of type %s" %
614617
ValType(val))
618+
elif self.options & NON_DATA_IS_NULL:
619+
self.buf.write('null')
615620
else:
616621
# Similar to = operator, ui.DebugPrint()
617622
# TODO: that prints value.Range in a special way

doc/ref/chap-builtin-cmd.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,10 @@ When a block is long, the former is more readable.
665665
Write JSON:
666666

667667
var d = {name: 'bob', age: 42}
668-
json write (d) # default indentation of 2
669-
json write (d, space=0) # no indentation
668+
json write (d) # default indent of 2, type errors
669+
json write (d, space=0) # no indent
670+
json write (d, type_errors=false) # non-serializable types become null
671+
# (e.g. Obj, Proc, Eggex)
670672

671673
Read JSON:
672674

doc/ref/chap-builtin-func.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,13 @@ Add indentation by passing the `space` param:
301301
$ = toJson([42], space=2)
302302
(Str) "[\n 42\n]"
303303

304-
Similar to `json write (x)`, except the default value of `space` is 0.
304+
Turn non-serializable types into `null`, instead of raising an error:
305+
306+
$ = toJson(/d+/, type_errors=false)
307+
(Str) 'null'
308+
309+
The `toJson()` function is to `json write (x)`, except the default value of
310+
`space` is 0.
305311

306312
See [err-json-encode][] for errors.
307313

spec/hay.test.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ Package cpython {
837837
#
838838
# When type_errors=false (default), any unserializable value becomes null
839839

840-
echo 'json write (_hay())' > stage-1.ysh
840+
echo 'json write (_hay(), type_errors=false)' > stage-1.ysh
841841

842842
# Stage 1
843843

@@ -867,7 +867,6 @@ build_proc
867867
;
868868

869869

870-
871870
## STDOUT:
872871
a
873872
## END

spec/ysh-json.test.sh

+34
Original file line numberDiff line numberDiff line change
@@ -1249,3 +1249,37 @@ json write (assoc)
12491249
}
12501250
}
12511251
## END
1252+
1253+
#### type_errors=false
1254+
1255+
var o = Obj.new({}, null)
1256+
1257+
#json write (o)
1258+
json write (o, type_errors=false)
1259+
1260+
var pat = /d+/
1261+
#json write (pat)
1262+
json write (pat, type_errors=false)
1263+
1264+
var d = {key: pat, key2: o}
1265+
json write (d, type_errors=false)
1266+
1267+
echo
1268+
1269+
echo $[toJson(o, type_errors=false)]
1270+
echo $[toJson8(o, type_errors=false)]
1271+
echo $[toJson8(d, type_errors=false)]
1272+
1273+
## STDOUT:
1274+
null
1275+
null
1276+
{
1277+
"key": null,
1278+
"key2": null
1279+
}
1280+
1281+
null
1282+
null
1283+
{"key":null,"key2":null}
1284+
## END
1285+

0 commit comments

Comments
 (0)