Jsonnet
repository·master·Indexed 11 days ago
https://github.com/google/jsonnetA data templating language designed for generating complex configuration files, such as Kubernetes manifests. It provides a programmable way to create JSON-like structures using variables, functions, and imports. Available as a CLI tool, C++ library, and Python bindings.
What's inside Jsonnet
- MathJax is an open-source JavaScript display engine used to render LaTeX, MathML, and AsciiMath notation in web browsers. It is designed to work across all modern browsers and operating systems without requiring users to install plugins or extra fonts. It provides high-quality mathematical typesetting, supports math accessibility, and offers a powerful API for integration into web applications.
Understand the purpose and use cases of Jsonnet
masterJsonnet is a data templating language designed primarily for configuring complex systems. It allows you to programmatically set up individual services to avoid massive duplication when integrating multiple services that do not know about each other.
Key use cases include:
- System Configuration: Specifying configuration on your terms to manage complex, multi-service environments.
- Application Configuration: Providing a generic configuration language for users that applications can consume as JSON or other structured formats.
- Static Site Generation: Using the language to generate structured data for sites.
- Embedded Expression Language: Integrating Jsonnet into other applications as a logic engine.
- Ad hoc JSON Transformations: Transforming JSON data (though
jqmay be preferred for one-off, terse tasks). - Teaching: Demonstrating a principled, simple approach to programming.
Understand Jsonnet expressions and evaluation
masterJsonnet programs are composed entirely of expressions. There are no statements or special top-level declarations. Common constructs like
import,if/elseconditionals,function,object, andlocalare all expressions.- Evaluation: Every expression evaluates to a value. Evaluation is side-effect free.
- Program Structure: A valid Jsonnet program does not need to be a top-level object; any expression is a valid program (e.g.,
2+2or"foo"). - Environment: The value of an expression depends on its environment (the values of the variables it refers to).
Understand the Jsonnet to Java transliteration mapping
masterWhen transliterating Jsonnet code to Java, the following structural mappings are applied:
- Typing: Everything is typed as
Object(due to Jsonnet's dynamic typing) and everything ispublic. - Arrays: Jsonnet arrays map to
Object[]. - Primitives: Jsonnet primitives map to
Boolean,Double, orString. - Objects: Jsonnet objects are represented as singleton instances of named Java classes that extend
JsonnetObject. - Fields: Jsonnet fields are converted into Java methods with no parameters (reflecting that fields are virtual in Jsonnet).
- Hidden Fields: The status of hidden fields is managed via a
nonHiddenFieldsmethod, which returns a set of field names. Any field not in this set is considered hidden.
Note on limitations: This transliteration does not properly escape output strings and prints JSON on a single line rather than using standard indentation.
- Typing: Everything is typed as
How Rapid YAML is vendored in this project
masterThis project uses the 'single header' release of Rapid YAML (version 0.10.0). The library is provided via therapidyaml-0.10.0.hppheader. Therapidyaml.cppfile is used to instantiate the library as a single translation unit.Framework components for Jsonnet manifestation in Java
masterThe Java implementation uses auxiliary framework classes to replicate Jsonnet's implicit behaviors:
Testclass: Responsible for selecting a specific object and manifesting it tostdout.JsonnetValueclass: Implements manifestation using a visitor pattern over possible JSON values to build JSON strings. For objects, it iterates overnonHiddenFieldsand manifests each value by reflectively calling the corresponding method.
Security considerations for untrusted input
masterThe C++ implementation of Jsonnet is not hardened for processing untrusted inputs (untrusted Jsonnet code). It is intended for evaluating code that you or your organization has written and trusts.
Risks:
- The
import,importstr, andimportbinlanguage constructs can be used to exfiltrate sensitive data. - By default, these constructs can import from any path accessible to the interpreter process.
If you must process untrusted code, consider using go-jsonnet or implementing a sandbox to restrict the interpreter's access.
- The
Working with Strings
masterStrings are sequences of Unicode codepoints. While they behave similarly to arrays in some contexts, they are distinct types.
- Array-like behavior: You can use
std.length()or the[]operator to treat a string as an array of single-codepoint strings. Comparison operators (<,<=,>,>=,==,!=) perform lexicographical comparison of codepoints. - Strictness: Unlike arrays, strings are strict; evaluating a string requires calculating all its contents immediately.
- Construction: Strings can be created via literals, slices, concatenations, or by converting an array of Unicode codepoint numbers.
// String as array-like "foo"[0] // "f" std.length("foo") // 3 // Comparison "a" < "b" // true- Array-like behavior: You can use
Equality and Equivalence in Jsonnet
masterJsonnet distinguishes between Equality (
==) and Equivalence.Equality (
==)- Values of different types are never equal (no implicit casting).
a == bevaluates totrueorfalse.- Note: Functions cannot be checked for equality. Consequently, arrays or objects containing functions may be neither equal nor unequal (they cannot be compared).
Equivalence
Two values are equivalent if they are indistinguishable by any Jsonnet function. Equivalent values may have different internal representations but will behave identically in all contexts.
Key distinction: All equal values are equivalent, but not all equivalent values are equal (e.g.,
{ a: 1, b: 1}and{a: 1, b: self.a}are equivalent but not equal).Jsonnet Value Types and Immutability
masterJsonnet supports seven fundamental value types. All values in Jsonnet are immutable; you cannot modify an existing object or array, you can only create a new one with the desired changes applied.
Supported Types:
null: A single value,null.boolean:trueandfalse.string: Unicode sequences.number: IEEE754 64-bit floating point numbers.function: Pure functions that take arguments and return a value.array: Finite-length sequences of values.object: Key-value mappings (a superset of JSON objects) with inheritance support.
You can determine the type of any value using
std.type(value).std.type(true) // "boolean" std.type("hello") // "string"Working with Numbers
masterJsonnet numbers are IEEE754 64-bit floating point numbers.
- Constraints:
nanandinfare not supported. Operations that would result in infinity or NaN will trigger an error. - Safe Integers: Integers are safe within the range
[-2^53 + 1, 2^53 - 1]. - Bitwise Operations: In C++ and Go implementations, bitwise operators (
<<,>>,&,|,^) are restricted to the safe integer range.
- Constraints:
Understand Jsonnet Hermeticity
masterJsonnet programs are pure computations. This means they have no side-effects and depend only on values explicitly passed to them. The behavior of a program is independent of the host system's environment (operating system, environment variables, or filesystem).
This hermeticity ensures:
- Predictability: Behavior won't change based on system setup.
- Portability: Programs run identically on development machines and CI/CD.
- Longevity: Code remains valid even as external technologies evolve.
Data from the environment can only be introduced to a program through explicit abstractions like Top-Level Arguments or External Variables.