Skip to content

Commit

Permalink
Add failing test for #112
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 16, 2024
1 parent 8d5be9e commit fa1acbb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void testMapWithEnumKey() throws Exception
WithEnumMap input = new WithEnumMap(DEF.E, "bar");
// verify serialization, should be ok:
String json = JSON.std.asString(input);
assertEquals(aposToQuotes("{'values':{'E':'bar'}}"), json);
assertEquals(a2q("{'values':{'E':'bar'}}"), json);

// and then get it back too
WithEnumMap result = JSON.std.beanFrom(WithEnumMap.class, json);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.fasterxml.jackson.jr.failing;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.jr.ob.*;
import com.fasterxml.jackson.jr.ob.api.*;
import com.fasterxml.jackson.jr.ob.impl.JSONWriter;

public class ValueWriterModifier112Test extends TestBase
{
static class TestBean112 {
public Path p1;
public Path p2;
}

static class PathWriter implements ValueWriter {
@Override
public void writeValue(JSONWriter context, JsonGenerator g, Object value) throws IOException {
g.writeString(((Path) value).toString().replace(File.separatorChar, '/'));
}

@Override
public Class<?> valueType() {
return Path.class;
}
}

// [jackson-jr#112]
public void testMultipleFieldOverrides() throws Exception
{
TestBean112 input = new TestBean112();
input.p1 = Paths.get("some/path");
input.p2 = Paths.get("some/other/path");

JSON writer = JSON.builder()
.register(new JacksonJrExtension() {
@Override
protected void register(ExtensionContext ctxt) {
ctxt.insertModifier(new ReaderWriterModifier() {
@Override
public ValueWriter overrideStandardValueWriter(JSONWriter writeContext, Class<?> type, int stdTypeId) {
if (type == Path.class) {
return new PathWriter();
}
return super.overrideStandardValueWriter(writeContext, type, stdTypeId);
}
});
ctxt.insertProvider(new ReaderWriterProvider() {
@Override
public ValueWriter findValueWriter(JSONWriter writeContext, Class<?> type) {
if (Path.class.isAssignableFrom(type)) {
return new PathWriter();
}
return super.findValueWriter(writeContext, type);
}
});
}
}).build();
String json = writer.asString(input);
assertEquals(a2q("{'p1':'some/path','p2':'some/other/path'}"), json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ protected String quote(String str) {
}

protected String a2q(String json) {
return aposToQuotes(json);
return json.replace('\'', '"');
}

@Deprecated
protected String aposToQuotes(String json) {
return json.replace("'", "\"");
return a2q(json);
}

protected JSON jsonWithModifier(final ReaderWriterModifier modifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public Class<?> valueType() {
String json = jsonWithModifier(mod).asString(input);
assertEquals(quote("Foo-Bar"), json);
// but also verify that no caching occurs wrt global standard variant:
assertEquals(aposToQuotes("{'first':'Foo','last':'Bar'}"),
assertEquals(a2q("{'first':'Foo','last':'Bar'}"),
JSON.std.asString(input));
}

Expand All @@ -145,10 +145,10 @@ public void testPOJOWriterDelegatingReplacement() throws Exception
final NameBean input = new NameBean("Foo", "Bar");
String json = jsonWithModifier(new ArrayingWriterModifier())
.asString(input);
assertEquals(aposToQuotes("[{'first':'Foo','last':'Bar'}]"), json);
assertEquals(a2q("[{'first':'Foo','last':'Bar'}]"), json);

// but also verify that no caching occurs wrt global standard variant:
assertEquals(aposToQuotes("{'first':'Foo','last':'Bar'}"),
assertEquals(a2q("{'first':'Foo','last':'Bar'}"),
JSON.std.asString(input));
}
}

0 comments on commit fa1acbb

Please sign in to comment.