Class JsonPatchMarshaller


public class JsonPatchMarshaller extends CharMarshaller
A pairing of a JsonSerializer and JsonParser into a single class with convenience to/of methods, pre-wired for JSON Patch's JsonPatchOperation discriminator.

Every JsonSerializer/JsonParser used to read or write JsonPatchOperation (or JsonPatch) needs to be configured with .typePropertyName(JsonPatchOperation.class, "op") so that polymorphic dispatch across AddOp, RemoveOp, ReplaceOp, MoveOp, CopyOp, and TestOp resolves via the RFC 6902 "op" member rather than the default "_type" property. This class hand-wires that configuration once so callers don't have to repeat it.

Examples:

// Using instance. JsonPatchMarshaller m = new JsonPatchMarshaller(); JsonPatch patch = m.read(string, JsonPatch.class); String string = m.write(patch);

// Using DEFAULT instance. JsonPatch patch = JsonPatchMarshaller.DEFAULT.read(string, JsonPatch.class); String string = JsonPatchMarshaller.DEFAULT.write(patch);

// Using static shortcuts. JsonPatch patch = JsonPatchMarshaller.to(string, JsonPatch.class); String string = JsonPatchMarshaller.of(patch);

Example output:

[{"op":"add","path":"/a/b/c","value":"foo"},{"op":"remove","path":"/a/b/c"}]

See Also:
  • Field Details

  • Constructor Details

    • JsonPatchMarshaller

      Creates using the pre-wired SERIALIZER and PARSER.
    • JsonPatchMarshaller

      Creates with custom serializer and parser.

      Callers overriding these should preserve the .typePropertyName(JsonPatchOperation.class, "op") configuration (and, on the serializer side, .addBeanTypes()/.addRootType()) or polymorphic dispatch across the JsonPatchOperation subclasses will not round-trip correctly.

      Parameters:
      s - The serializer.
      p - The parser.
  • Method Details

    • of

      public static String of(Object object) throws SerializeException
      Serializes a POJO to a String using the DEFAULT marshaller.

      A shortcut for calling DEFAULT.write(object).

      Parameters:
      object - The object to serialize.
      Returns:
      The serialized object.
      Throws:
      SerializeException - If a problem occurred trying to convert the output.
    • to

      public static <T> T to(String input, Class<T> type) throws ParseException
      Parses an input into the specified object type using the DEFAULT marshaller.

      A shortcut for calling DEFAULT.read(input, type).

      Type Parameters:
      T - The class type of the object being created.
      Parameters:
      input - The input.
      type - The object type to create.
      Returns:
      The parsed object.
      Throws:
      ParseException - Malformed input encountered.
    • to

      public static <T> T to(String input, Type type, Type... args) throws ParseException
      Parses an input into the specified parameterized object type using the DEFAULT marshaller.

      A shortcut for calling DEFAULT.read(input, type, args).

      Type Parameters:
      T - The class type of the object to create.
      Parameters:
      input - The input.
      type - The object type to create.
      args - The type arguments of the class if it's a collection or map.
      Returns:
      The parsed object.
      Throws:
      ParseException - Malformed input encountered.
    • to

      public static <T> T to(Reader input, Class<T> type) throws ParseException
      Parses the contents of a Reader into the specified object type using the DEFAULT marshaller.

      A shortcut for calling DEFAULT.read(input, type) that catches any IOException from the underlying stream and rethrows it as an unchecked ParseException, so the caller is not burdened with a checked exception.

      Type Parameters:
      T - The class type of the object being created.
      Parameters:
      input - The input reader.
      type - The object type to create.
      Returns:
      The parsed object.
      Throws:
      ParseException - Malformed input encountered or an I/O error occurred on the underlying stream.
    • to

      public static <T> T to(Reader input, Type type, Type... args) throws ParseException
      Parses the contents of a Reader into the specified parameterized object type using the DEFAULT marshaller.

      A shortcut for calling DEFAULT.read(input, type, args) that catches any IOException from the underlying stream and rethrows it as an unchecked ParseException, so the caller is not burdened with a checked exception.

      Type Parameters:
      T - The class type of the object to create.
      Parameters:
      input - The input reader.
      type - The object type to create.
      args - The type arguments of the class if it's a collection or map.
      Returns:
      The parsed object.
      Throws:
      ParseException - Malformed input encountered or an I/O error occurred on the underlying stream.
    • of

      public static void of(Object object, Writer output) throws SerializeException
      Serializes a POJO to the specified Writer using the DEFAULT marshaller.

      A shortcut for calling DEFAULT.write(object, output) that catches any IOException from the underlying stream and rethrows it as an unchecked SerializeException, so the caller is not burdened with a checked exception.

      Parameters:
      object - The object to serialize.
      output - The writer to serialize to.
      Throws:
      SerializeException - If a problem occurred trying to convert the output or an I/O error occurred on the underlying stream.