Skip to content

Commit 1db5c9d

Browse files
committed
Issue #11 - Simplify serialization of raw values by using ObjectMapper#writeValueAsString().
Relates-to: FasterXML/jackson-databind#743
1 parent 3d024c8 commit 1db5c9d

File tree

3 files changed

+25
-54
lines changed

3 files changed

+25
-54
lines changed

src/main/java/de/agilecoders/wicket/jquery/util/Json.java

+23-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.agilecoders.wicket.jquery.util;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import com.fasterxml.jackson.databind.JavaType;
45
import com.fasterxml.jackson.databind.JsonNode;
56
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -31,6 +32,23 @@ private Json() {
3132
throw new UnsupportedOperationException();
3233
}
3334

35+
/**
36+
* Convert a string to a Java value
37+
*
38+
* @param json Json value to convert.
39+
* @param type Expected Java value type.
40+
* @param <T> type of return object
41+
* @return casted value of given json object
42+
* @throws ParseException to runtime if json node can't be casted to clazz.
43+
*/
44+
public static <T> T fromJson(final String json, final JavaType type) {
45+
try {
46+
return createObjectMapper().readValue(json, type);
47+
} catch (Exception e) {
48+
throw new ParseException(e);
49+
}
50+
}
51+
3452
/**
3553
* @return a new {@link ObjectMapper} instance which allows single
3654
* quotes and unquoted keys by default
@@ -75,23 +93,6 @@ public static <T> T fromJson(final JsonNode json, final Class<T> clazz) {
7593
}
7694
}
7795

78-
/**
79-
* Convert a string to a Java value
80-
*
81-
* @param json Json value to convert.
82-
* @param type Expected Java value type.
83-
* @param <T> type of return object
84-
* @return casted value of given json object
85-
* @throws ParseException to runtime if json node can't be casted to clazz.
86-
*/
87-
public static <T> T fromJson(final String json, final JavaType type) {
88-
try {
89-
return createObjectMapper().readValue(json, type);
90-
} catch (Exception e) {
91-
throw new ParseException(e);
92-
}
93-
}
94-
9596
/**
9697
* Convert a JsonNode to a Java value
9798
*
@@ -121,7 +122,11 @@ public static ObjectNode newObject() {
121122
* @return stringified version of given json object
122123
*/
123124
public static String stringify(final JsonNode json) {
124-
return json != null ? json.toString() : "{}";
125+
try {
126+
return json != null ? createObjectMapper().writeValueAsString(json) : "{}";
127+
} catch (JsonProcessingException jpx) {
128+
throw new RuntimeException("A problem occurred while stringifying a JsonNode: " + jpx.getMessage(), jpx);
129+
}
125130
}
126131

127132
/**

src/main/java/de/agilecoders/wicket/jquery/util/serializer/ConfigSerializer.java

+1-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.fasterxml.jackson.core.JsonGenerator;
44
import com.fasterxml.jackson.databind.JsonSerializer;
55
import com.fasterxml.jackson.databind.SerializerProvider;
6-
import com.fasterxml.jackson.databind.util.RawValue;
76
import de.agilecoders.wicket.jquery.Config;
87

98
import java.io.IOException;
@@ -16,22 +15,6 @@
1615
public class ConfigSerializer extends JsonSerializer<Config> {
1716
@Override
1817
public void serialize(Config value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
19-
jsonGenerator.writeObject(new RV(value.toJsonString()));
20-
}
21-
22-
/**
23-
* An extension of com.fasterxml.jackson.databind.util.RawValue that delegates
24-
* toString() to the rawValue
25-
*/
26-
private static class RV extends RawValue
27-
{
28-
public RV(String v) {
29-
super(v);
30-
}
31-
32-
@Override
33-
public String toString() {
34-
return rawValue().toString();
35-
}
18+
jsonGenerator.writeRawValue(value.toJsonString());
3619
}
3720
}

src/main/java/de/agilecoders/wicket/jquery/util/serializer/RawSerializer.java

+1-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.fasterxml.jackson.core.JsonGenerator;
44
import com.fasterxml.jackson.databind.JsonSerializer;
55
import com.fasterxml.jackson.databind.SerializerProvider;
6-
import com.fasterxml.jackson.databind.util.RawValue;
76
import de.agilecoders.wicket.jquery.util.Json;
87

98
import java.io.IOException;
@@ -16,22 +15,6 @@
1615
public class RawSerializer extends JsonSerializer<Json.RawValue> {
1716
@Override
1817
public void serialize(Json.RawValue value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
19-
jsonGenerator.writeObject(new RV(value.value()));
20-
}
21-
22-
/**
23-
* An extension of com.fasterxml.jackson.databind.util.RawValue that delegates
24-
* toString() to the rawValue
25-
*/
26-
private static class RV extends RawValue {
27-
28-
public RV(String v) {
29-
super(v);
30-
}
31-
32-
@Override
33-
public String toString() {
34-
return rawValue().toString();
35-
}
18+
jsonGenerator.writeRawValue(value.value());
3619
}
3720
}

0 commit comments

Comments
 (0)