Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Afterburner doesn't handle annotation @JsonRawValue while serializing to JSON #38

Closed
sfc-gh-amotivala opened this issue Dec 9, 2013 · 4 comments
Milestone

Comments

@sfc-gh-amotivala
Copy link

The @ JsonRawValue annotation in the test below is ignored, and its quoted and treated like a string. Does the library support raw serialization? If yes, how would one go about it?

import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import java.io.StringWriter;
import org.junit.Test;

import static junit.framework.TestCase.assertEquals;

public class JSONUtilTest
{

  class SerializableObject
  {
    @JsonRawValue
    String value;

   public String getValue()
   {
     return value;
   }
  }

  public String genJSON(ObjectMapper mapper, Object obj) throws Exception
  {
    StringWriter sw = new StringWriter();
    String dataValue;

    JsonGenerator jsonGenerator = new JsonFactory().createJsonGenerator(sw);    

    // start top level object
    jsonGenerator.writeStartObject();
    jsonGenerator.writeFieldName("test");
    dataValue = mapper.writeValueAsString(obj);
    jsonGenerator.writeRawValue(dataValue);

    // end top level object
    jsonGenerator.writeEndObject();
    jsonGenerator.close();

    sw.close();
    String actual = sw.getBuffer().toString();
    return actual;
  }

  @Test
  public void testAfterBurner() throws Exception
  {
    SerializableObject so = new SerializableObject();
    so.value = "{  k1:1, k2:2 }";

    ObjectMapper mapper1 = new ObjectMapper();
    assertEquals("{\"test\":{\"value\":{  k1:1, k2:2 }}}", genJSON(mapper1, so));

    ObjectMapper mapper2 = new ObjectMapper();
    mapper2.registerModule(new AfterburnerModule());

    // Afterburner bug, it ignores the JsonRawValue annotation, and quotes the 
    // JsonRawValue, treating like a string. Afterburner code seems to be 
    // missing a raw serializer.
    assertEquals("{\"test\":{\"value\":\"{  k1:1, k2:2 }\"}}", 
                 genJSON(mapper2, so));
  }  
}
@cowtowncoder
Copy link
Member

I think raw values should work same with Afterburner as they would do without; that is, work if underlying format supports it via JsonGenerator. So this sounds like a bug.

@sfc-gh-amotivala
Copy link
Author

Yep. I'm working around it in the SerializerModifier, when it checks for custom serializers.

protected boolean isDefaultButNotRawSerializer(SerializationConfig config,
        JsonSerializer<?> ser)
{
    boolean ret = ClassUtil.isJacksonStdImpl(ser);
    // hack to force afterburner to ignore Raw Serializers
    boolean ret2 = ser instanceof RawSerializer;
    return ret && !ret2;
}

@cowtowncoder
Copy link
Member

I hope it's just a simple oversight, and something that can be fixed for 2.3.1.

@cowtowncoder
Copy link
Member

Yes, problem was one reported. I actually changed it by removing @JacksonStdImpl from RawSerializer (which shouldn't have had it).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants