Skip to content

Commit

Permalink
[ysh/builtin] toJson() toJson8() accepts space=
Browse files Browse the repository at this point in the history
The default value is zero.  We're going to change json write to have a
default value of 2.

And get rid of flags --pretty=F and --indent=0
  • Loading branch information
Andy Chu committed Apr 17, 2024
1 parent d06f197 commit 57aea62
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 16 deletions.
14 changes: 12 additions & 2 deletions builtin/func_misc.py
Expand Up @@ -414,14 +414,21 @@ def Call(self, rd):
# type: (typed_args.Reader) -> value_t

val = rd.PosValue()
space = mops.BigTruncate(rd.NamedInt('space', 0))
rd.Done()

# Convert from external JS-like API to internal.
if space <= 0:
indent = -1
else:
indent = space

buf = mylib.BufWriter()
try:
if self.is_j8:
j8.PrintMessage(val, buf, -1)
j8.PrintMessage(val, buf, indent)
else:
j8.PrintJsonMessage(val, buf, -1)
j8.PrintJsonMessage(val, buf, indent)
except error.Encode as e:
# status code 4 is special, for encode/decode errors.
raise error.Structured(4, e.Message(), rd.LeftParenToken())
Expand Down Expand Up @@ -457,3 +464,6 @@ def Call(self, rd):
raise error.Structured(4, e.Message(), rd.LeftParenToken(), props)

return val


# vim: sw=2
11 changes: 8 additions & 3 deletions doc/ref/chap-builtin-func.md
Expand Up @@ -319,10 +319,15 @@ Note, you will need to `source --builtin list.ysh` to use this function.

Convert an object in memory to JSON text:

= toJson({name: "alice"})
(Str) '{"name": "alice"}'
$ = toJson({name: "alice"})
(Str) '{"name":"alice"}'

Similar to `json write ({name: "alice"})`.
Add indentation by passing the `space` param:

$ = toJson([42], space=2)
(Str) "[\n 42\n]"

Similar to `json write (x)`, except the default value of `space` is 0.

See [json-encode-err](chap-errors.html#json-encode-err) for errors.

Expand Down
48 changes: 39 additions & 9 deletions spec/ysh-json.test.sh
Expand Up @@ -63,7 +63,6 @@ json write ([{k: 'v', k2: 'v2'}, {}])
#### json write compact format
shopt --set parse_proc

# TODO: ORDER of keys should be PRESERVED
var mydict = {name: "bob", age: 30}

json write --pretty=0 (mydict)
Expand Down Expand Up @@ -600,28 +599,59 @@ for j in '"\ud83e"' '"\udd26"' {
"\udd26"
## END

#### toJson() toJson8() - TODO: test difference
#### toJson() toJson8()

var obj = [42, 1.5, null, true, "hi"]
var obj = [42, 1.5, null, true, "hi", b'\yf0']

echo $[toJson(obj)]
echo $[toJson8(obj)]

var obj2 = [3, 4]
echo $[toJson(obj2, space=0)] # same as the default
echo $[toJson8(obj2, space=0)]

echo $[toJson(obj2, space=2)]
echo $[toJson8(obj2, space=2)]

# fully specify this behavior
echo $[toJson(obj2, space=-2)]
echo $[toJson8(obj2, space=-2)]

## STDOUT:
[42,1.5,null,true,"hi"]
[42,1.5,null,true,"hi"]
[42,1.5,null,true,"hi",""]
[42,1.5,null,true,"hi",b'\yf0']
[3,4]
[3,4]
[
3,
4
]
[
3,
4
]
[3,4]
[3,4]
## END

#### fromJson() fromJson8() - TODO: test difference
#### fromJson() fromJson8()

var message ='[42,1.5,null,true,"hi"]'
var m1 = '[42,1.5,null,true,"hi"]'

pp line (fromJson(message))
pp line (fromJson8(message))
# JSON8 message
var m2 = '[42,1.5,null,true,"hi",' ++ "u''" ++ ']'

pp line (fromJson8(m1))
pp line (fromJson(m1))

pp line (fromJson8(m2))
pp line (fromJson(m2)) # fails

## status: 4
## STDOUT:
(List) [42,1.5,null,true,"hi"]
(List) [42,1.5,null,true,"hi"]
(List) [42,1.5,null,true,"hi",""]
## END

#### User can handle errors - toJson() toJson8()
Expand Down
5 changes: 3 additions & 2 deletions ysh/expr_to_ast.py
Expand Up @@ -805,8 +805,9 @@ def _CheckLhs(self, lhs):

else:
# Illegal - e.g. setglobal {}["key"] = 42
p_die("Subscript and Attribute not allowed on this LHS expression",
location.TokenForExpr(lhs))
p_die(
"Subscript/Attribute not allowed on this LHS expression",
location.TokenForExpr(lhs))

def _LhsExprList(self, p_node):
# type: (PNode) -> List[y_lhs_t]
Expand Down

0 comments on commit 57aea62

Please sign in to comment.