Skip to content

Commit

Permalink
Fixed #630
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jul 9, 2020
1 parent be5af88 commit 3440c73
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ JSON library.
#619: Add `StreamReadCapability` for further format-based/format-agnostic
handling improvements
#627: Add `JsonParser.isExpectedNumberIntToken()` convenience method
#630: Add `StreamWriteCapability` for further format-based/format-agnostic
handling improvements
- Deprecate `JsonParser.getCurrentTokenId()` (use `#currentTokenId()` instead)

2.11.1 (25-Jun-2020)
Expand Down
43 changes: 42 additions & 1 deletion src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.fasterxml.jackson.core.io.CharacterEscapes;
import com.fasterxml.jackson.core.type.WritableTypeId;
import com.fasterxml.jackson.core.type.WritableTypeId.Inclusion;
import com.fasterxml.jackson.core.util.JacksonFeatureSet;
import com.fasterxml.jackson.core.util.VersionUtil;

import static com.fasterxml.jackson.core.JsonTokenId.*;
Expand All @@ -29,6 +30,34 @@
public abstract class JsonGenerator
implements Closeable, Flushable, Versioned
{
/**
* Default set of {@link StreamReadCapability}ies that may be used as
* basis for format-specific readers (or as bogus instance if non-null
* set needs to be passed).
*
* @since 2.12
*/
protected final static JacksonFeatureSet<StreamWriteCapability> DEFAULT_WRITE_CAPABILITIES
= JacksonFeatureSet.fromDefaults(StreamWriteCapability.values());

/**
* Default set of {@link StreamReadCapability}ies for typical textual formats,
* to use either as-is, or as a base with possible differences.
*
* @since 2.12
*/
protected final static JacksonFeatureSet<StreamWriteCapability> DEFAULT_TEXTUAL_WRITE_CAPABILITIES
= DEFAULT_WRITE_CAPABILITIES.with(StreamWriteCapability.CAN_WRITE_FORMATTED_NUMBERS);

/**
* Default set of {@link StreamReadCapability}ies for typical binary formats,
* to use either as-is, or as a base with possible differences.
*
* @since 2.12
*/
protected final static JacksonFeatureSet<StreamWriteCapability> DEFAULT_BINARY_WRITE_CAPABILITIES
= DEFAULT_WRITE_CAPABILITIES.with(StreamWriteCapability.CAN_WRITE_BINARY_NATIVELY);

/**
* Enumeration that defines all togglable features for generators.
*/
Expand Down Expand Up @@ -634,7 +663,7 @@ public void setCurrentValue(Object v) {
ctxt.setCurrentValue(v);
}
}

/*
/**********************************************************
/* Public API, capability introspection methods
Expand Down Expand Up @@ -719,6 +748,18 @@ public void setCurrentValue(Object v) {
*/
public boolean canWriteFormattedNumbers() { return false; }

/**
* Accessor for getting metadata on capabilities of this parser, based on
* underlying data format being read (directly or indirectly).
*
* @return Set of read capabilities for content to read via this parser
*
* @since 2.12
*/
public JacksonFeatureSet<StreamWriteCapability> getWriteCapabilities() {
return DEFAULT_WRITE_CAPABILITIES;
}

/*
/**********************************************************
/* Public API, write methods, structural
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.fasterxml.jackson.core;

import com.fasterxml.jackson.core.util.JacksonFeature;

/**
* Set of on/off capabilities that a {@link JsonGenerator} for given format
* (or in case of buffering, original format) has.
* Used in some cases to adjust aspects of things like content conversions and
* coercions by format-agnostic functionality.
* Specific or expected usage documented by individual capability entry Javadocs.
*
* @since 2.12
*/
public enum StreamWriteCapability
implements JacksonFeature
{
/**
* Capability that indicates that the data format is able to express binary
* data natively, without using textual encoding like Base64.
*<p>
* Capability is currently enabled for all binary formats and none of textual
* formats.
*/
CAN_WRITE_BINARY_NATIVELY(false),

/**
* Capability that indicates that the data format is able to write
* "formatted numbers": that is, output of numbers is done as Strings
* and caller is allowed to pass in logical number values as Strings.
*<p>
* Capability is currently enabled for most textual formats and none of binary
* formats.
*/
CAN_WRITE_FORMATTED_NUMBERS(false)
;

/**
* Whether feature is enabled or disabled by default.
*/
private final boolean _defaultState;

private final int _mask;

private StreamWriteCapability(boolean defaultState) {
_defaultState = defaultState;
_mask = (1 << ordinal());
}

@Override
public boolean enabledByDefault() { return _defaultState; }
@Override
public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
@Override
public int getMask() { return _mask; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.core.io.CharacterEscapes;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.core.util.JacksonFeatureSet;
import com.fasterxml.jackson.core.util.VersionUtil;

/**
Expand All @@ -30,6 +31,15 @@ public abstract class JsonGeneratorImpl extends GeneratorBase
*/
protected final static int[] sOutputEscapes = CharTypes.get7BitOutputEscapes();

/**
* Default capabilities for JSON generator implementations which do not
* different from "general textual" defaults
*
* @since 2.12
*/
protected final static JacksonFeatureSet<StreamWriteCapability> JSON_WRITE_CAPABILITIES
= DEFAULT_TEXTUAL_WRITE_CAPABILITIES;

/*
/**********************************************************
/* Configuration, basic I/O
Expand Down Expand Up @@ -192,6 +202,11 @@ public JsonGenerator setRootValueSeparator(SerializableString sep) {
return this;
}

@Override
public JacksonFeatureSet<StreamWriteCapability> getWriteCapabilities() {
return JSON_WRITE_CAPABILITIES;
}

/*
/**********************************************************
/* Shared helper methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ public void setCurrentValue(Object v) {
@Override
public boolean canWriteFormattedNumbers() { return delegate.canWriteFormattedNumbers(); }

@Override
public JacksonFeatureSet<StreamWriteCapability> getWriteCapabilities() {
return delegate.getWriteCapabilities();
}

/*
/**********************************************************
/* Public API, configuration
Expand Down

0 comments on commit 3440c73

Please sign in to comment.