-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add RawValue
helper type
#743
Comments
…eFactory` (for ObjectNode, ArrayNode)
Additional work done:
So: it should be possible to inject raw values into |
…e when needing to serialize as raw X-Refs: - FasterXML/jackson-databind#737 - FasterXML/jackson-databind#743
Hi, I've just tried to use |
@martin-g Unless there is specific reason to do so, you should call |
@cowtowncoder I would like to use |
@martin-g Hmmh. How exactly are you calling So I guess a code snippet would be helpful here in understanding the problem. |
See martin-g/wicket-jquery-selectors@ac61f56#diff-370a00d3f658d65a011325ed28899f48R18. Here I try to serialize my own custom type It should be easy for you to reproduce the problem by cloning https://github.com/l0rdn1kk0n/wicket-jquery-selectors, checkout branch |
@martin-g ok I will try to diagnose what is going on here. |
@martin-g Hmmh. I don't understand this -- I pointed that the method to use is "writeRawValue()" and NOT "writeObject()". Yet code is:
so the proper call would be jsonGenerator.writeRawValue(value.value()); But it also seems unnecessary to add custom serializer, given that |
@cowtowncoder Here is a self-contained test case: import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* Unit test for simple App.
*/
public class AppTest {
@Test
public void jacksonRawValue() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new MyModule());
MyType myType = new MyType("Jackson");
Map<String, Object> object = new HashMap<String, Object>();
object.put("key", myType);
JsonNode jsonNode = objectMapper.valueToTree(object);
String json = jsonNode.toString();
Assert.assertEquals("{\"key\":Jackson}", json);
}
public static class MyTypeSerializer extends JsonSerializer<MyType> {
@Override
public void serialize(MyType myType, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
jsonGenerator.writeRawValue(myType.value());
}
}
public static class MyType {
private final String value;
public MyType(String value) {
this.value = value;
}
public String value() {
return value;
}
}
private static class MyModule extends SimpleModule {
public MyModule() {
super("jackson-raw-value-test");
addSerializer(MyType.class, new MyTypeSerializer());
}
}
} It uses 2.6.0-SNAPSHOT from Sonatype OSS snapshots: <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.0-SNAPSHOT</version>
</dependency> As I explained earlier I needed to use |
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.RawValue;
import org.junit.Assert;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
/**
* Unit test for simple App.
*/
public class AppTest {
@Test
public void jacksonRawValue() {
ObjectMapper objectMapper = new ObjectMapper();
RawValue myType = new RawValue("Jackson");
Map<String, Object> object = new HashMap<String, Object>();
object.put("key", myType);
JsonNode jsonNode = objectMapper.valueToTree(object);
String json = jsonNode.toString();
Assert.assertEquals("{\"key\":Jackson}", json);
}
} fails the same way:
|
Thank you, I will confirm I can see the failure. |
Yes, I can confirm this. Looks like this is related to intermediate |
Ok. So I did find one underlying bug, which prevented a proper raw value containing node from getting added. But there is also a problem with the test: it assumes that Having said that, I will go ahead and see if I can improve |
… propagated, improve `POJONode.toString()` a bit
I improved output of |
|
@martin-g good! |
…r#writeValueAsString(). Relates-to: FasterXML/jackson-databind#743
(note: related to #737, #348)
Supporting handling of "raw values" is tricky due to various reasons. But one possibly workable way would be to allow passing of raw values as
JsonToken.VALUE_EMBEDDED_OBJECT
of specific type.Since there is nothing existing that quite fits the use case it is probably best to just add a simple value type in
com.fasterxml.jackson.databind.util
, say, "JsonRawValue".It should be possible to construct from
java.lang.String
, but possible also from other sources to support alternate forms ofwriteRawValue()
.The main method for accessing data should be one to simply write contents using given
JsonGenerator
, but alternate accessors may be added as well.Once this type is added, it should be possible to work on end-to-end handling of raw values, to support use cases where such values need to be passed through various conversions.
The text was updated successfully, but these errors were encountered: