Class Json

java.lang.Object
dev.dokimos.core.internal.Json

public final class Json extends Object
Shared, framework-internal JSON utilities backed by a single, immutable Jackson ObjectMapper.

The mapper is constructed once in a static initializer and is never mutated afterwards. Only the thread-safe ObjectReader and ObjectWriter views are exposed (directly or via convenience helpers), which keeps all usage safe under the parallel experiment runs that dokimos-core performs.

Configuration of note:

  • StreamReadFeature.STRICT_DUPLICATE_DETECTION is enabled on the comparison reader so duplicate object keys (a real signal in an evaluation) fail loudly rather than being silently collapsed.
  • DeserializationFeature.FAIL_ON_TRAILING_TOKENS is enabled so trailing garbage after a value is rejected.
  • Non-finite floating point values (NaN, Infinity) are rejected when parsing (the JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS read feature is disabled). Jackson has no equivalent throw-on-write for non-finite doubles, so the structural comparator additionally rejects in-memory non-finite values before comparison; this keeps Jackson's surprising NaN == NaN out of eval results.

This class is internal API. It is not part of the public, supported surface of dokimos-core and may change without notice.

  • Method Summary

    Modifier and Type
    Method
    Description
    static com.fasterxml.jackson.databind.ObjectWriter
    A thread-safe writer producing compact (single-line) JSON.
    static com.fasterxml.jackson.databind.ObjectReader
    A thread-safe reader configured for structural comparison: strict duplicate-key detection is enabled so {"x":1,"x":2} fails rather than silently collapsing.
    static <T> T
    convert(Object value, com.fasterxml.jackson.databind.JavaType type)
    Converts a value into an instance described by a Jackson JavaType, supporting generic targets such as List<Foo>.
    static <T> T
    convert(Object value, Class<T> type)
    Converts a value into an instance of type without an intermediate textual round-trip.
    static com.fasterxml.jackson.databind.ObjectWriter
    A thread-safe writer producing pretty-printed JSON.
    static <T> T
    read(String json, com.fasterxml.jackson.core.type.TypeReference<T> type)
    Parses a JSON string into the generic target captured by a Jackson TypeReference, using the strict comparison reader so duplicate object keys are rejected alongside the NaN/Infinity and trailing-token tightening that applies to every reader.
    static <T> T
    read(String json, com.fasterxml.jackson.databind.JavaType type)
    Parses a JSON string into an instance described by a Jackson JavaType, supporting generic targets such as List<Foo>.
    static <T> T
    read(String json, Class<T> type)
    Parses a JSON string into an instance of type.
    static com.fasterxml.jackson.databind.ObjectReader
    A thread-safe reader over the shared, immutable mapper.
    static com.fasterxml.jackson.databind.JavaType
    Resolves a Jackson JavaType from a generic Type.
    static com.fasterxml.jackson.databind.JsonNode
    toNode(Object value)
    Converts an arbitrary value to a JsonNode tree (the in-memory model the structural comparator works against).
    static String
    Serializes a value to compact (single-line) JSON.
    static String
    Serializes a value to pretty-printed JSON.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • reader

      public static com.fasterxml.jackson.databind.ObjectReader reader()
      A thread-safe reader over the shared, immutable mapper. Use for general value conversion.
      Returns:
      the shared ObjectReader
    • comparisonReader

      public static com.fasterxml.jackson.databind.ObjectReader comparisonReader()
      A thread-safe reader configured for structural comparison: strict duplicate-key detection is enabled so {"x":1,"x":2} fails rather than silently collapsing.
      Returns:
      the shared comparison ObjectReader
    • compactWriter

      public static com.fasterxml.jackson.databind.ObjectWriter compactWriter()
      A thread-safe writer producing compact (single-line) JSON.
      Returns:
      the shared compact ObjectWriter
    • prettyWriter

      public static com.fasterxml.jackson.databind.ObjectWriter prettyWriter()
      A thread-safe writer producing pretty-printed JSON.
      Returns:
      the shared pretty-printing ObjectWriter
    • convert

      public static <T> T convert(Object value, Class<T> type)
      Converts a value into an instance of type without an intermediate textual round-trip.
      Type Parameters:
      T - the target type
      Parameters:
      value - the source value (may be null)
      type - the target class
      Returns:
      the converted value, or null if value is null
      Throws:
      IllegalArgumentException - if the value cannot be converted to type
    • convert

      public static <T> T convert(Object value, com.fasterxml.jackson.databind.JavaType type)
      Converts a value into an instance described by a Jackson JavaType, supporting generic targets such as List<Foo>.
      Type Parameters:
      T - the target type
      Parameters:
      value - the source value (may be null)
      type - the target Jackson type
      Returns:
      the converted value, or null if value is null
      Throws:
      IllegalArgumentException - if the value cannot be converted to type
    • toNode

      public static com.fasterxml.jackson.databind.JsonNode toNode(Object value)
      Converts an arbitrary value to a JsonNode tree (the in-memory model the structural comparator works against).
      Parameters:
      value - the source value (may be null)
      Returns:
      the value as a JsonNode; a null value yields a Jackson null node
    • writeCompact

      public static String writeCompact(Object value)
      Serializes a value to compact (single-line) JSON.
      Parameters:
      value - the value to serialize
      Returns:
      the compact JSON string
      Throws:
      IllegalArgumentException - if the value cannot be serialized
    • writePretty

      public static String writePretty(Object value)
      Serializes a value to pretty-printed JSON.
      Parameters:
      value - the value to serialize
      Returns:
      the pretty-printed JSON string
      Throws:
      IllegalArgumentException - if the value cannot be serialized
    • read

      public static <T> T read(String json, Class<T> type)
      Parses a JSON string into an instance of type.

      Backed by reader().forType(type), which returns a fresh, type-bound view of the shared immutable mapper without mutating it — so this is safe under the parallel experiment runs dokimos-core performs.

      Type Parameters:
      T - the target type
      Parameters:
      json - the JSON text to parse
      type - the target class
      Returns:
      the parsed value (the JSON literal null parses to null)
      Throws:
      IllegalArgumentException - if the text is not valid JSON or does not match type
    • read

      public static <T> T read(String json, com.fasterxml.jackson.databind.JavaType type)
      Parses a JSON string into an instance described by a Jackson JavaType, supporting generic targets such as List<Foo>.

      Backed by reader().forType(type), which returns a fresh, type-bound view of the shared immutable mapper without mutating it — so this is safe under the parallel experiment runs dokimos-core performs.

      Type Parameters:
      T - the target type
      Parameters:
      json - the JSON text to parse
      type - the target Jackson type
      Returns:
      the parsed value (the JSON literal null parses to null)
      Throws:
      IllegalArgumentException - if the text is not valid JSON or does not match type
    • read

      public static <T> T read(String json, com.fasterxml.jackson.core.type.TypeReference<T> type)
      Parses a JSON string into the generic target captured by a Jackson TypeReference, using the strict comparison reader so duplicate object keys are rejected alongside the NaN/Infinity and trailing-token tightening that applies to every reader.

      This is the load-path entry point for dataset and server JSON: malformed structured input (a duplicate key, a NaN token, trailing garbage) fails here at parse time rather than surfacing confusingly later during evaluation.

      Type Parameters:
      T - the target type
      Parameters:
      json - the JSON text to parse
      type - the captured generic target type
      Returns:
      the parsed value (the JSON literal null parses to null)
      Throws:
      IllegalArgumentException - if the text is not valid JSON or does not match type
    • resolveType

      public static com.fasterxml.jackson.databind.JavaType resolveType(Type type)
      Resolves a Jackson JavaType from a generic Type. Used to bridge an OutputType<T>'s captured type into the conversion machinery.
      Parameters:
      type - the generic type to resolve
      Returns:
      the corresponding Jackson JavaType