Package org.microproxy.starlark.stdlib
Class JsonModule
java.lang.Object
org.microproxy.starlark.stdlib.JsonModule
- All Implemented Interfaces:
org.microproxy.thirdparty.starlark.eval.StarlarkValue
public final class JsonModule
extends Object
implements org.microproxy.thirdparty.starlark.eval.StarlarkValue
@stdlib//json: the interpreter's json module (Json, the same one scripts
have predeclared), with Python's names dumps and loads, and Python's refusal to
encode bytes and sets. As in Starlark, object keys are written in sorted order.-
Method Summary
Modifier and TypeMethodDescriptiondecode(String x, Object defaultValue, org.microproxy.thirdparty.starlark.eval.StarlarkThread thread) Parses JSON, optionally returning a fallback for malformed input.Encodes a value using Python-style optional indentation.Encodes a value as compact JSON with sorted object keys.encodeIndent(Object x, String prefix, String indent, org.microproxy.thirdparty.starlark.eval.StarlarkThread thread) Encodes a value as JSON and formats it with indentation.Reformats JSON with a line prefix and indentation unit.Parses JSON from text or strictly decoded UTF-8 bytes.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.microproxy.thirdparty.starlark.eval.StarlarkValue
checkHashable, debugPrint, getStarlarkType, isAcyclic, isImmutable, repr, str, truth
-
Method Details
-
encode
public String encode(Object x, org.microproxy.thirdparty.starlark.eval.StarlarkThread thread) throws org.microproxy.thirdparty.starlark.eval.EvalException, InterruptedException Encodes a value as compact JSON with sorted object keys.- Parameters:
x- the Starlark value to encodethread- the calling interpreter thread- Returns:
- the JSON text
- Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- if the value cannot be represented as JSONInterruptedException- if encoding is interrupted
-
dumps
public String dumps(Object x, Object indent, org.microproxy.thirdparty.starlark.eval.StarlarkThread thread) throws org.microproxy.thirdparty.starlark.eval.EvalException, InterruptedException Encodes a value using Python-style optional indentation.- Parameters:
x- the Starlark value to encodeindent- the number of spaces, an indentation string orNonefor compact outputthread- the calling interpreter thread- Returns:
- the JSON text
- Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- if the value or indentation cannot be encodedInterruptedException- if encoding is interrupted
-
decode
public Object decode(String x, Object defaultValue, org.microproxy.thirdparty.starlark.eval.StarlarkThread thread) throws org.microproxy.thirdparty.starlark.eval.EvalException Parses JSON, optionally returning a fallback for malformed input.- Parameters:
x- the JSON textdefaultValue- the fallback, orStarlark.UNBOUNDto report invalid JSONthread- the calling thread that owns decoded containers- Returns:
- the decoded Starlark value or the supplied fallback
- Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- if JSON is invalid and no fallback was supplied
-
loads
public Object loads(Object s, org.microproxy.thirdparty.starlark.eval.StarlarkThread thread) throws org.microproxy.thirdparty.starlark.eval.EvalException Parses JSON from text or strictly decoded UTF-8 bytes.- Parameters:
s- the JSON string or bytesthread- the calling thread that owns decoded containers- Returns:
- the decoded Starlark value
- Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- if UTF-8 decoding or JSON parsing fails
-
indent
public String indent(String s, String prefix, String indent) throws org.microproxy.thirdparty.starlark.eval.EvalException Reformats JSON with a line prefix and indentation unit.- Parameters:
s- the JSON text to reformatprefix- the prefix for output linesindent- the indentation added per nesting level- Returns:
- the reformatted JSON text
- Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- if the input is invalid JSON
-
encodeIndent
public String encodeIndent(Object x, String prefix, String indent, org.microproxy.thirdparty.starlark.eval.StarlarkThread thread) throws org.microproxy.thirdparty.starlark.eval.EvalException, InterruptedException Encodes a value as JSON and formats it with indentation.- Parameters:
x- the Starlark value to encodeprefix- the prefix for output linesindent- the indentation added per nesting levelthread- the calling interpreter thread- Returns:
- the formatted JSON text
- Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- if the value cannot be represented as JSONInterruptedException- if encoding is interrupted
-