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 Type
    Method
    Description
    decode(String x, Object defaultValue, org.microproxy.thirdparty.starlark.eval.StarlarkThread thread)
    Parses JSON, optionally returning a fallback for malformed input.
    dumps(Object x, Object indent, org.microproxy.thirdparty.starlark.eval.StarlarkThread thread)
    Encodes a value using Python-style optional indentation.
    encode(Object x, org.microproxy.thirdparty.starlark.eval.StarlarkThread thread)
    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.
    indent(String s, String prefix, String indent)
    Reformats JSON with a line prefix and indentation unit.
    loads(Object s, org.microproxy.thirdparty.starlark.eval.StarlarkThread thread)
    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, wait

    Methods 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 encode
      thread - the calling interpreter thread
      Returns:
      the JSON text
      Throws:
      org.microproxy.thirdparty.starlark.eval.EvalException - if the value cannot be represented as JSON
      InterruptedException - 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 encode
      indent - the number of spaces, an indentation string or None for compact output
      thread - the calling interpreter thread
      Returns:
      the JSON text
      Throws:
      org.microproxy.thirdparty.starlark.eval.EvalException - if the value or indentation cannot be encoded
      InterruptedException - 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 text
      defaultValue - the fallback, or Starlark.UNBOUND to report invalid JSON
      thread - 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 bytes
      thread - 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 reformat
      prefix - the prefix for output lines
      indent - 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 encode
      prefix - the prefix for output lines
      indent - the indentation added per nesting level
      thread - the calling interpreter thread
      Returns:
      the formatted JSON text
      Throws:
      org.microproxy.thirdparty.starlark.eval.EvalException - if the value cannot be represented as JSON
      InterruptedException - if encoding is interrupted