Skip to content

Commit

Permalink
Continuing with ReaderWriterModifier, issue #70
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatu Saloranta committed Dec 17, 2019
1 parent 6c26cc1 commit c73029a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 17 deletions.
33 changes: 26 additions & 7 deletions jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.fasterxml.jackson.core.util.Instantiatable;
import com.fasterxml.jackson.jr.ob.api.CollectionBuilder;
import com.fasterxml.jackson.jr.ob.api.MapBuilder;
import com.fasterxml.jackson.jr.ob.api.ReaderWriterModifier;
import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider;
import com.fasterxml.jackson.jr.ob.comp.CollectionComposer;
import com.fasterxml.jackson.jr.ob.comp.ComposerBase;
Expand Down Expand Up @@ -212,7 +213,7 @@ public enum Feature

/*
/**********************************************************************
/* Features that affect introspection
/* Features that affect introspection and thereby affect caching
/**********************************************************************
*/

Expand Down Expand Up @@ -422,18 +423,20 @@ protected JSON(int features,
_features = features;
_jsonFactory = jsonF;
_treeCodec = trees;
_reader = (r != null) ? r : _defaultReader(features, trees, null);
_writer = (w != null) ? w : _defaultWriter(features, trees, null);
_reader = (r != null) ? r : _defaultReader(features, trees, null, null);
_writer = (w != null) ? w : _defaultWriter(features, trees, null, null);
_prettyPrinter = pp;
}

protected JSONReader _defaultReader(int features, TreeCodec tc, ReaderWriterProvider rwp) {
return new JSONReader(features, ValueReaderLocator.blueprint(features, rwp), tc,
protected JSONReader _defaultReader(int features, TreeCodec tc,
ReaderWriterProvider rwp, ReaderWriterModifier rwm) {
return new JSONReader(features, ValueReaderLocator.blueprint(features, rwp, rwm), tc,
CollectionBuilder.defaultImpl(), MapBuilder.defaultImpl());
}

protected JSONWriter _defaultWriter(int features, TreeCodec tc, ReaderWriterProvider rwp) {
return new JSONWriter(features, ValueWriterLocator.blueprint(features, rwp), tc);
protected JSONWriter _defaultWriter(int features, TreeCodec tc,
ReaderWriterProvider rwp, ReaderWriterModifier rwm) {
return new JSONWriter(features, ValueWriterLocator.blueprint(features, rwp, rwm), tc);
}

/*
Expand Down Expand Up @@ -568,6 +571,22 @@ public JSON with(ReaderWriterProvider rwp) {
return _with(_features, _jsonFactory, _treeCodec,
r, w, _prettyPrinter);
}

/**
* Mutant factory for constructing an instance with specified {@link ReaderWriterModifier},
* and returning new instance (or, if there would be no change, this instance).
*
* @since 2.11
*/
public JSON with(ReaderWriterModifier rwm) {
JSONReader r = _reader.with(rwm);
JSONWriter w = _writer.with(rwm);
if ((r == _reader) && (w == _writer)) {
return this;
}
return _with(_features, _jsonFactory, _treeCodec,
r, w, _prettyPrinter);
}

/**
* Mutant factory for constructing an instance with specified feature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.jr.ob.JSONObjectException;
import com.fasterxml.jackson.jr.ob.api.CollectionBuilder;
import com.fasterxml.jackson.jr.ob.api.MapBuilder;
import com.fasterxml.jackson.jr.ob.api.ReaderWriterModifier;
import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider;
import com.fasterxml.jackson.jr.ob.api.ValueReader;

Expand Down Expand Up @@ -120,6 +121,15 @@ public JSONReader with(ReaderWriterProvider rwp) {
return _with(_features, l, _treeCodec, _collectionBuilder, _mapBuilder);
}

// @since 2.11
public JSONReader with(ReaderWriterModifier rwm) {
ValueReaderLocator l = _readerLocator.with(rwm);
if (_readerLocator == l) {
return this;
}
return _with(_features, l, _treeCodec, _collectionBuilder, _mapBuilder);
}

/**
* Overridable method that all mutant factories call if a new instance
* is to be constructed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.fasterxml.jackson.core.io.SerializedString;
import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.ob.JSONObjectException;
import com.fasterxml.jackson.jr.ob.api.ReaderWriterModifier;
import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider;
import com.fasterxml.jackson.jr.ob.api.ValueWriter;

Expand Down Expand Up @@ -110,6 +111,7 @@ public JSONWriter with(TreeCodec tc) {
return _with(_features, _writerLocator, tc);
}

// @since 2.10
public JSONWriter with(ReaderWriterProvider rwp) {
ValueWriterLocator l = _writerLocator.with(rwp);
if (_writerLocator == l) {
Expand All @@ -118,6 +120,15 @@ public JSONWriter with(ReaderWriterProvider rwp) {
return _with(_features, l, _treeCodec);
}

// @since 2.11
public JSONWriter with(ReaderWriterModifier rwm) {
ValueWriterLocator l = _writerLocator.with(rwm);
if (_writerLocator == l) {
return this;
}
return _with(_features, l, _treeCodec);
}

/**
* Overridable method that all mutant factories call if a new instance
* is to be constructed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.concurrent.ConcurrentHashMap;

import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.ob.api.ReaderWriterModifier;
import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider;
import com.fasterxml.jackson.jr.ob.api.ValueReader;
import com.fasterxml.jackson.jr.type.ResolvedType;
Expand Down Expand Up @@ -51,6 +52,13 @@ public class ValueReaderLocator
*/
protected final ReaderWriterProvider _readerProvider;

/**
* Provider for reader customizer, if any; may be null.
*
* @since 2.11
*/
protected final ReaderWriterModifier _readerModifier;

/*
/**********************************************************************
/* Caching
Expand Down Expand Up @@ -107,10 +115,12 @@ public class ValueReaderLocator
/**
* Constructor for the blueprint instance
*/
protected ValueReaderLocator(int features, ReaderWriterProvider rwp)
protected ValueReaderLocator(int features,
ReaderWriterProvider rwp, ReaderWriterModifier rwm)
{
_features = features;
_readerProvider = rwp;
_readerModifier = rwm;
_knownReaders = new ConcurrentHashMap<ClassKey, ValueReader>(10, 0.75f, 2);
_typeResolver = new TypeResolver();
_readerLock = new Object();
Expand All @@ -122,33 +132,45 @@ protected ValueReaderLocator(ValueReaderLocator base, int features,
_features = features;
_readContext = r;
_readerProvider = base._readerProvider;
_readerModifier = base._readerModifier;
_knownReaders = base._knownReaders;
_typeResolver = base._typeResolver;
_readerLock = base._readerLock;
}

protected ValueReaderLocator(ValueReaderLocator base, ReaderWriterProvider rwp) {
protected ValueReaderLocator(ValueReaderLocator base,
ReaderWriterProvider rwp, ReaderWriterModifier rwm)
{
// create new cache as there may be custom writers:
_knownReaders = new ConcurrentHashMap<ClassKey, ValueReader>(10, 0.75f, 2);

_features = base._features;
_readContext = base._readContext;
_readerProvider = rwp;
_readerModifier = rwm;
_typeResolver = base._typeResolver;
_readerLock = base._readerLock;
}

public final static ValueReaderLocator blueprint(int features, ReaderWriterProvider rwp) {
return new ValueReaderLocator(features & CACHE_FLAGS, rwp);
public final static ValueReaderLocator blueprint(int features,
ReaderWriterProvider rwp, ReaderWriterModifier rwm) {
return new ValueReaderLocator(features & CACHE_FLAGS, rwp, rwm);
}

public ValueReaderLocator with(ReaderWriterProvider rwp) {
if (rwp == _readerProvider) {
return this;
}
return new ValueReaderLocator(this, rwp);
return new ValueReaderLocator(this, rwp, _readerModifier);
}

public ValueReaderLocator with(ReaderWriterModifier rwm) {
if (rwm == _readerModifier) {
return this;
}
return new ValueReaderLocator(this, _readerProvider, rwm);
}

public ValueReaderLocator perOperationInstance(JSONReader r, int features) {
return new ValueReaderLocator(this, features & CACHE_FLAGS, r);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.concurrent.CopyOnWriteArrayList;

import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.ob.api.ReaderWriterModifier;
import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider;
import com.fasterxml.jackson.jr.ob.api.ValueWriter;

Expand Down Expand Up @@ -46,6 +47,13 @@ public class ValueWriterLocator extends ValueLocatorBase
*/
protected final ReaderWriterProvider _writerProvider;

/**
* Provider for reader customizer, if any; may be null.
*
* @since 2.11
*/
protected final ReaderWriterModifier _writerModifier;

/*
/**********************************************************************
/* Instance configuration
Expand Down Expand Up @@ -80,13 +88,15 @@ public class ValueWriterLocator extends ValueLocatorBase
/**
* Constructor for the blueprint instance
*/
protected ValueWriterLocator(int features, ReaderWriterProvider rwp)
protected ValueWriterLocator(int features,
ReaderWriterProvider rwp, ReaderWriterModifier rwm)
{
_features = features;
_knownSerTypes = new ConcurrentHashMap<ClassKey, Integer>(20, 0.75f, 2);
_knownWriters = new CopyOnWriteArrayList<ValueWriter>();
_writeContext = null;
_writerProvider = rwp;
_writerModifier = rwm;
}

// for per-call instances
Expand All @@ -97,19 +107,28 @@ protected ValueWriterLocator(ValueWriterLocator base,
_knownSerTypes = base._knownSerTypes;
_knownWriters = base._knownWriters;
_writerProvider = base._writerProvider;
_writerModifier = base._writerModifier;
}

public final static ValueWriterLocator blueprint(int features,
ReaderWriterProvider rwp) {
return new ValueWriterLocator(features & CACHE_FLAGS, rwp);
ReaderWriterProvider rwp, ReaderWriterModifier rwm) {
return new ValueWriterLocator(features & CACHE_FLAGS, rwp, rwm);
}

public ValueWriterLocator with(ReaderWriterProvider rwp) {
if (rwp == _writerProvider) {
return this;
}
// nothing much to reuse if so, use blueprint ctor
return new ValueWriterLocator(_features, rwp);
return new ValueWriterLocator(_features, rwp, _writerModifier);
}

public ValueWriterLocator with(ReaderWriterModifier rwm) {
if (rwm == _writerModifier) {
return this;
}
// nothing much to reuse if so, use blueprint ctor
return new ValueWriterLocator(_features, _writerProvider, rwm);
}

public ValueWriterLocator perOperationInstance(JSONWriter w, int features) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void setValue(Long v) {
*/

public void testBasicTypeDetectionForSer() {
ValueWriterLocator td = ValueWriterLocator.blueprint(JSON.Feature.defaults(), null);
ValueWriterLocator td = ValueWriterLocator.blueprint(JSON.Feature.defaults(), null, null);
assertEquals(ValueWriterLocator.SER_STRING, td.findSerializationType(String.class));
assertEquals(ValueWriterLocator.SER_CHAR_ARRAY, td.findSerializationType(char[].class));
assertEquals(ValueWriterLocator.SER_INT_ARRAY, td.findSerializationType(int[].class));
Expand Down

0 comments on commit c73029a

Please sign in to comment.