Skip to content
RuedigerMoeller edited this page May 25, 2015 · 34 revisions

Json serialization

(released with 2.29)

The Json codec provides the possibility to do full fledged Object Graph serialization based on JSon format. Other than existing solutions (e.g. Jackson Databind or Boon) it is fully precise (Object Graph is identical after deserialization) and supports/restores cyclic and interlinked object graphs same as serialization. It uses Jackson-Core under the hood to parse/generate Json, so its FST's serialization implementation using a Jackson-back end instead of byte arrays for in/output.

Ofc there is a cost for maintaining full type information, so performance is reasonable, however for simple non cyclic datastructures, Json libs are faster (at least the performance leaders jackson/boon). However once data gets more complex, frequently manual translation Application Data <=> Json Intermediate Message representation has to be done (nobody adds this to benchmarks :-) ).

Use case:

  • cross platform/language data exchange.
  • productivity. Leverage existing interfaces by just switching the codec from native serialization to Json.
  • readability/historical safety. Can be read+processed by any tool. Original classes are not required.
  • generalization+abstraction. E.g. Kontraktor 3.0 provides pluggable networking (TCP, WebSockets, Http) in combination with different flavors of data encoding without the need to change anything in application code.

Features

  • Serializes Serializable and Externalizable object graphs to Json keeping precise type information same as JDK and FST Serialization
  • Support for cyclic and linked data structures same as serialization
  • Flat mode (ignore references in object graph) supported
  • Reuse existing serialization code to support Json.

Limits/Issues

  • embedded type information produces somewhat ugly Json output (btw: machines don't mind ;) ). Additionally there is a performance cost for additional type information tags.
  • classes using old school JDK serialization implementations (readObject/writeObject/readReplace..) are not supported, one needs to register FST-Serializers for those. Fortunately fst comes with a broad range of predefined+preconfigured Serializers (Collections, frequently used Data Types), so for most cases it will work out of the box same as regular FST Serialization.

How Objects get formatted

static class SimpleClass implements Serializable {
    String name = "You";
    double aDouble = 13.3456;
    int anInt;
}

public static void main(String[] args) throws UnsupportedEncodingException {
    FSTConfiguration conf = FSTConfiguration.createJsonConfiguration();
    Object p = new SimpleClass();

    byte[] bytes = conf.asByteArray(p);
    System.out.println(new String(bytes,"UTF-8"));
    Object deser = conf.asObject(bytes);
}

yields:

{
  "type": "ser.Play$SimpleClass",
  "obj": {
    "aDouble": 13.3456,
    "anInt": 0,
    "name": "You"
  }
}
Clone this wiki locally