| 1 |
package com.fasterxml.jackson.jr.ob.impl; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.util.Collections; |
| 5 |
|
| 6 |
import com.fasterxml.jackson.core.JsonGenerator; |
| 7 |
import com.fasterxml.jackson.jr.ob.*; |
| 8 |
import com.fasterxml.jackson.jr.ob.api.*; |
| 9 |
|
| 10 |
public class CustomValueWritersTest extends TestBase |
| 11 |
{ |
| 12 |
static class CustomBean { } |
| 13 |
|
| 14 |
static class CustomBeanAsStringWriter implements ValueWriter { |
| 15 |
public final String text; |
| 16 |
|
| 17 |
public CustomBeanAsStringWriter(String v) { text = v; } |
| 18 |
|
| 19 |
@Override |
| 20 |
public void writeValue(JSONWriter context, JsonGenerator g, |
| 21 |
Object v) throws IOException { |
| 22 |
g.writeString(text); |
| 23 |
} |
| 24 |
|
| 25 |
@Override |
| 26 |
public Class<?> valueType() { return CustomBean.class; } |
| 27 |
} |
| 28 |
|
| 29 |
static class CustomBeanAsAnswerWriter implements ValueWriter { |
| 30 |
@Override |
| 31 |
public void writeValue(JSONWriter context, JsonGenerator g, |
| 32 |
Object v) throws IOException { |
| 33 |
g.writeStartObject(); |
| 34 |
g.writeNumberField("answer", 42); |
| 35 |
g.writeNullField("none"); |
| 36 |
g.writeEndObject(); |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
public Class<?> valueType() { return CustomBean.class; } |
| 41 |
} |
| 42 |
|
| 43 |
static class CustomBeanWrapper { |
| 44 |
public CustomBean wrapped = new CustomBean(); |
| 45 |
} |
| 46 |
|
| 47 |
static class CustomWriters extends ReaderWriterProvider { |
| 48 |
private final String _str; |
| 49 |
|
| 50 |
public CustomWriters() { this("xxx"); } |
| 51 |
public CustomWriters(String str) { _str = str; } |
| 52 |
|
| 53 |
@Override |
| 54 |
public ValueWriter findValueWriter(JSONWriter writeContext, Class<?> type) { |
| 55 |
if (type == CustomBean.class) { |
| 56 |
return new CustomBeanAsStringWriter(_str); |
| 57 |
} |
| 58 |
return null; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
static class CustomWriters42 extends ReaderWriterProvider { |
| 63 |
@Override |
| 64 |
public ValueWriter findValueWriter(JSONWriter writeContext, Class<?> type) { |
| 65 |
if (type == CustomBean.class) { |
| 66 |
return new CustomBeanAsAnswerWriter(); |
| 67 |
} |
| 68 |
return null; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
static class BogusProvider extends ReaderWriterProvider { |
| 73 |
} |
| 74 |
|
| 75 |
/* |
| 76 |
/********************************************************************** |
| 77 |
/* Test methdods |
| 78 |
/********************************************************************** |
| 79 |
*/ |
| 80 |
|
| 81 |
public void testCustomBeanWriter() throws Exception |
| 82 |
{ |
| 83 |
// without handler, empty "bean": |
| 84 |
assertEquals("{}", JSON.std.asString(new CustomBean())); |
| 85 |
assertEquals("{\"wrapped\":{}}", JSON.std.asString(new CustomBeanWrapper())); |
| 86 |
|
| 87 |
final JSON withCustom = jsonWithProvider(new CustomWriters()); |
| 88 |
assertEquals(quote("xxx"), withCustom.asString(new CustomBean())); |
| 89 |
assertEquals("{\"wrapped\":\"xxx\"}", withCustom.asString(new CustomBeanWrapper())); |
| 90 |
assertEquals("[\"xxx\"]", withCustom.asString(new CustomBean[] { new CustomBean() })); |
| 91 |
assertEquals("{\"value\":\"xxx\"}", |
| 92 |
withCustom.asString(Collections.singletonMap("value", new CustomBean()))); |
| 93 |
|
| 94 |
// plus also should be able to create new instance with different representation |
| 95 |
final JSON withCustom42 = jsonWithProvider(new CustomWriters42()); |
| 96 |
assertEquals("{\"answer\":42,\"none\":null}", withCustom42.asString(new CustomBean())); |
| 97 |
assertEquals("{\"wrapped\":{\"answer\":42,\"none\":null}}", |
| 98 |
withCustom42.asString(new CustomBeanWrapper())); |
| 99 |
} |
| 100 |
|
| 101 |
public void testChainedBeanWriters() throws Exception |
| 102 |
{ |
| 103 |
assertEquals(quote("abc"), |
| 104 |
jsonWithProviders(new CustomWriters("abc"), new CustomWriters("def")) |
| 105 |
.asString(new CustomBean())); |
| 106 |
assertEquals(quote("def"), |
| 107 |
jsonWithProviders(new BogusProvider(), new CustomWriters("def")) |
| 108 |
.asString(new CustomBean())); |
| 109 |
|
| 110 |
// as well as passing `null` |
| 111 |
assertEquals(quote("xxx"), |
| 112 |
jsonWithProviders(null, new CustomWriters("xxx")) |
| 113 |
.asString(new CustomBean())); |
| 114 |
assertEquals(quote("yyy"), |
| 115 |
jsonWithProviders(new CustomWriters("yyy"), null) |
| 116 |
.asString(new CustomBean())); |
| 117 |
} |
| 118 |
} |