Skip to content

Commit

Permalink
Implemented reader (deserialization) side of #65
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 8, 2019
1 parent be7385e commit 153a169
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,20 @@ protected ValueReader(Class<?> valueType) {
* Method called to deserialize value of type supported by this reader, using
* given parser. Parser is not yet positioned to the (first) token
* of the value to read and needs to be advanced.
*<p>
* Default implementation simply calls `p.nextToken()` first, then calls
* {#link {@link #read(JSONReader, JsonParser)}, but some implementations
* may decide to implement this differently to use (slightly) more efficient
* accessor in {@link JsonParser}, like {@link JsonParser#nextIntValue(int)}.
*
* @param reader Context object that allows calling other read methods for contained
* values of different types (for example for collection readers).
* @param p Underlying parser used for reading decoded token stream
*/
public abstract Object readNext(JSONReader reader, JsonParser p) throws IOException;
public Object readNext(JSONReader reader, JsonParser p) throws IOException {
p.nextToken();
return read(reader, p);
}

/*
/**********************************************************************
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.fasterxml.jackson.jr.ob.impl;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.ob.JSONObjectException;
import com.fasterxml.jackson.jr.ob.TestBase;
import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider;
import com.fasterxml.jackson.jr.ob.api.ValueReader;

public class CustomValueHandlersTest extends TestBase
{
static class CustomValue {
public int value;

// 2nd arg just to avoid discovery
public CustomValue(int v, boolean b) {
// and to ensure it goes through constructor, add 1
value = v + 1;
}
}

static class CustomValueBean {
public CustomValue custom;

protected CustomValueBean() { }
public CustomValueBean(int v) {
custom = new CustomValue(v, false);
}
}

static class CustomValueReader extends ValueReader {
public CustomValueReader() {
super(CustomValue.class);
}

@Override
public Object read(JSONReader reader, JsonParser p) throws IOException {
return new CustomValue(p.getIntValue(), true);
}

// Base class impl should be fine, although we'd use this for optimal
/*
@Override
public Object readNext(JSONReader reader, JsonParser p) throws IOException {
return new CustomValue(p.nextIntValue(-1), true);
}
*/
}

static class CustomReaders extends ReaderWriterProvider {
@Override
public ValueReader findBeanReader(JSONReader readContext, Class<?> type) {
if (type.equals(CustomValue.class)) {
return new CustomValueReader();
}
return null;
}
}

/*
/**********************************************************************
/* Test methdods
/**********************************************************************
*/

public void testSimpleCustomReader() throws Exception
{
// First: without handler, will fail to map
try {
JSON.std.beanFrom(CustomValue.class, "123");
fail("Should not pass");
} catch (JSONObjectException e) {
verifyException(e, ".CustomValue");
verifyException(e, "constructor to use");
}

// then with custom, should be fine
JSON json = JSON.std
.with(new CustomReaders());
CustomValue v = json.beanFrom(CustomValue.class, "123");
assertEquals(124, v.value);

// similarly with wrapper
CustomValueBean bean = json.beanFrom(CustomValueBean.class,
aposToQuotes("{ 'custom' : 137 }"));
assertEquals(138, bean.custom.value);
}
}

0 comments on commit 153a169

Please sign in to comment.