Skip to content

Commit

Permalink
Implement #28 (add serializers for JsonPatch, JsonMergePatch)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 30, 2022
1 parent 8fb2081 commit d5871fe
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.module.SimpleDeserializers;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.util.Converter;
import com.fasterxml.jackson.databind.util.StdConverter;

import jakarta.json.*;
import jakarta.json.spi.JsonProvider;
Expand All @@ -34,6 +37,10 @@ public JSONPModule(JsonProvider jsonProvider) {
final JsonMergePatchDeserializer jsonMergePatchDeser = new JsonMergePatchDeserializer(jsonValueDeser);

addSerializer(JsonValue.class, new JsonValueSerializer());
// Since 2.14.2
addSerializer(JsonPatch.class, new JsonPatchSerializer());
addSerializer(JsonMergePatch.class, new JsonMergePatchSerializer());

setDeserializers(new SimpleDeserializers() {
@Override
public JsonDeserializer<?> findBeanDeserializer(
Expand Down Expand Up @@ -93,4 +100,68 @@ public boolean hasDeserializerFor(DeserializationConfig config, Class<?> valueTy
}
});
}

/**
* @since 2.14.2
*/
static class JsonPatchSerializer extends StdDelegatingSerializer
{
private static final long serialVersionUID = 1L;

public JsonPatchSerializer() {
super(new PatchConverter());
}

protected JsonPatchSerializer(Converter<Object,?> converter,
JavaType delegateType, JsonSerializer<?> delegateSerialize) {
super(converter, delegateType, delegateSerialize);
}

@Override
protected StdDelegatingSerializer withDelegate(Converter<Object,?> converter,
JavaType delegateType, JsonSerializer<?> delegateSerializer) {
return new JsonPatchSerializer(converter, delegateType, delegateSerializer);
}

static class PatchConverter
extends StdConverter<JsonPatch,JsonArray>
{
@Override
public JsonArray convert(JsonPatch value) {
return value.toJsonArray();
}
}
}

/**
* @since 2.14.2
*/
static class JsonMergePatchSerializer extends StdDelegatingSerializer
{
private static final long serialVersionUID = 1L;

public JsonMergePatchSerializer() {
super(new PatchConverter());
}

protected JsonMergePatchSerializer(Converter<Object,?> converter,
JavaType delegateType, JsonSerializer<?> delegateSerialize) {
super(converter, delegateType, delegateSerialize);
}

@Override
protected StdDelegatingSerializer withDelegate(Converter<Object,?> converter,
JavaType delegateType, JsonSerializer<?> delegateSerializer) {
return new JsonMergePatchSerializer(converter, delegateType, delegateSerializer);
}

static class PatchConverter
extends StdConverter<JsonMergePatch, JsonValue>
{
@Override
public JsonValue convert(JsonMergePatch value) {
return value.toJsonValue();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.fasterxml.jackson.datatype.jsonp;

import com.fasterxml.jackson.databind.ObjectMapper;

import jakarta.json.JsonMergePatch;

public class JsonMergePatchSerializationTest extends TestBase
{
private static final ObjectMapper MAPPER = newMapper();

public void testSimpleSerialization() throws Exception
{
// First need a patch so deserialization must work
final String input = "{" +
"\"name\":\"Json\"" +
"}";
final JsonMergePatch patch1 = MAPPER.readValue(input, JsonMergePatch.class);
final String output = MAPPER.writeValueAsString(patch1);

// and read back
final JsonMergePatch patch2 = MAPPER.readValue(output, JsonMergePatch.class);
assertEquals(patch1.toJsonValue(), patch2.toJsonValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.fasterxml.jackson.datatype.jsonp;

import com.fasterxml.jackson.databind.ObjectMapper;

import jakarta.json.JsonPatch;

public class JsonPatchSerializationTest extends TestBase
{
private static final ObjectMapper MAPPER = newMapper();

public void testSimpleSerialization() throws Exception
{
// First need a patch so deserialization must work
final String input = "[" +
"{" +
"\"op\":\"replace\"," +
"\"path\":\"/name\"," +
"\"value\":\"Json\"" +
"}" +
"]";
final JsonPatch jsonPatch = MAPPER.readValue(input, JsonPatch.class);
final String output = MAPPER.writeValueAsString(jsonPatch);

// and read back
final JsonPatch jsonPatch2 = MAPPER.readValue(output, JsonPatch.class);
assertEquals(jsonPatch, jsonPatch2);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.fasterxml.jackson.datatype.jsr353;

import javax.json.*;
import javax.json.spi.JsonProvider;
import java.util.Collections;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.module.SimpleDeserializers;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.MapType;

import javax.json.*;
import javax.json.spi.JsonProvider;
import java.util.Collections;
import com.fasterxml.jackson.databind.util.Converter;
import com.fasterxml.jackson.databind.util.StdConverter;

public class JSR353Module extends SimpleModule
{
Expand All @@ -35,6 +38,10 @@ public JSR353Module(JsonProvider jsonProvider)
final JsonMergePatchDeserializer jsonMergePatchDeser = new JsonMergePatchDeserializer(jsonValueDeser);

addSerializer(JsonValue.class, new JsonValueSerializer());
// Since 2.14.2
addSerializer(JsonPatch.class, new JsonPatchSerializer());
addSerializer(JsonMergePatch.class, new JsonMergePatchSerializer());

setDeserializers(new SimpleDeserializers() {
@Override
public JsonDeserializer<?> findBeanDeserializer(
Expand Down Expand Up @@ -94,4 +101,68 @@ public boolean hasDeserializerFor(DeserializationConfig config, Class<?> valueTy
}
});
}

/**
* @since 2.14.2
*/
static class JsonPatchSerializer extends StdDelegatingSerializer
{
private static final long serialVersionUID = 1L;

public JsonPatchSerializer() {
super(new PatchConverter());
}

protected JsonPatchSerializer(Converter<Object,?> converter,
JavaType delegateType, JsonSerializer<?> delegateSerialize) {
super(converter, delegateType, delegateSerialize);
}

@Override
protected StdDelegatingSerializer withDelegate(Converter<Object,?> converter,
JavaType delegateType, JsonSerializer<?> delegateSerializer) {
return new JsonPatchSerializer(converter, delegateType, delegateSerializer);
}

static class PatchConverter
extends StdConverter<JsonPatch,JsonArray>
{
@Override
public JsonArray convert(JsonPatch value) {
return value.toJsonArray();
}
}
}

/**
* @since 2.14.2
*/
static class JsonMergePatchSerializer extends StdDelegatingSerializer
{
private static final long serialVersionUID = 1L;

public JsonMergePatchSerializer() {
super(new PatchConverter());
}

protected JsonMergePatchSerializer(Converter<Object,?> converter,
JavaType delegateType, JsonSerializer<?> delegateSerialize) {
super(converter, delegateType, delegateSerialize);
}

@Override
protected StdDelegatingSerializer withDelegate(Converter<Object,?> converter,
JavaType delegateType, JsonSerializer<?> delegateSerializer) {
return new JsonMergePatchSerializer(converter, delegateType, delegateSerializer);
}

static class PatchConverter
extends StdConverter<JsonMergePatch, JsonValue>
{
@Override
public JsonValue convert(JsonMergePatch value) {
return value.toJsonValue();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.fasterxml.jackson.datatype.jsr353;

import javax.json.JsonMergePatch;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonMergePatchSerializationTest extends TestBase
{
private static final ObjectMapper MAPPER = newMapper();

public void testSimpleSerialization() throws Exception
{
// First need a patch so deserialization must work
final String input = "{" +
"\"name\":\"Json\"" +
"}";
final JsonMergePatch patch1 = MAPPER.readValue(input, JsonMergePatch.class);
final String output = MAPPER.writeValueAsString(patch1);

// and read back
final JsonMergePatch patch2 = MAPPER.readValue(output, JsonMergePatch.class);
assertEquals(patch1.toJsonValue(), patch2.toJsonValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.fasterxml.jackson.datatype.jsr353;

import javax.json.JsonPatch;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonPatchSerializationTest extends TestBase
{
private static final ObjectMapper MAPPER = newMapper();

public void testSimpleSerialization() throws Exception
{
// First need a patch so deserialization must work
final String input = "[" +
"{" +
"\"op\":\"replace\"," +
"\"path\":\"/name\"," +
"\"value\":\"Json\"" +
"}" +
"]";
final JsonPatch jsonPatch = MAPPER.readValue(input, JsonPatch.class);
final String output = MAPPER.writeValueAsString(jsonPatch);

// and read back
final JsonPatch jsonPatch2 = MAPPER.readValue(output, JsonPatch.class);
assertEquals(jsonPatch, jsonPatch2);
}
}
5 changes: 5 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ Andrzej Pańkowski (apankowski@github)

* Contributed #17: Add configurable amount representations for Joda Money module
(2.14.0)

Matt Nelson (mattnelson@github)

* Suggested #28: Add delegating serializers for `JsonPatch` and `JsonMergePatch`
(2.14.2)
5 changes: 5 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Modules:
=== Releases ===
------------------------------------------------------------------------

2.14.2 (not yet released)

#28: Add delegating serializers for `JsonPatch` and `JsonMergePatch`
(requested by Matt N)

2.14.1 (21-Nov-2022)

No changes since 2.14.0
Expand Down

0 comments on commit d5871fe

Please sign in to comment.