com.github.nmorel.gwtjackson.client.JsonSerializationContext Maven / Gradle / Ivy
Show all versions of gwt-jackson Show documentation
/*
* Copyright 2013 Nicolas Morel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.nmorel.gwtjackson.client;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.fasterxml.jackson.annotation.ObjectIdGenerator;
import com.github.nmorel.gwtjackson.client.exception.JsonSerializationException;
import com.github.nmorel.gwtjackson.client.ser.bean.AbstractBeanJsonSerializer;
import com.github.nmorel.gwtjackson.client.ser.bean.ObjectIdSerializer;
import com.github.nmorel.gwtjackson.client.stream.JsonWriter;
import com.github.nmorel.gwtjackson.client.stream.impl.FastJsonWriter;
import com.google.gwt.core.client.GWT;
/**
* Context for the serialization process.
*
* @author Nicolas Morel
*/
public class JsonSerializationContext extends JsonMappingContext {
/**
* Builder for {@link JsonSerializationContext}. To override default settings globally, you can extend this class, modify the
* default settings inside the constructor and tell the compiler to use your builder instead in your gwt.xml file :
*
* {@code
*
*
*
*
*
* }
*
*/
public static class Builder {
protected boolean useEqualityForObjectId = false;
protected boolean serializeNulls = true;
protected boolean writeDatesAsTimestamps = true;
protected boolean writeDateKeysAsTimestamps = false;
protected boolean indent = false;
protected boolean wrapRootValue = false;
protected boolean writeCharArraysAsJsonArrays = false;
protected boolean writeNullMapValues = true;
protected boolean writeEmptyJsonArrays = true;
protected boolean orderMapEntriesByKeys = false;
protected boolean writeSingleElemArraysUnwrapped = false;
protected boolean wrapExceptions = true;
/**
* @deprecated Use {@link JsonSerializationContext#builder()} instead. This constructor will be made protected in v1.0.
*/
@Deprecated
public Builder() { }
/**
* Determines whether Object Identity is compared using
* true JVM-level identity of Object (false); or, equals()
method.
* Latter is sometimes useful when dealing with Database-bound objects with
* ORM libraries (like Hibernate).
*
* Option is disabled by default; meaning that strict identity is used, not
* equals()
*
*/
public Builder useEqualityForObjectId( boolean useEqualityForObjectId ) {
this.useEqualityForObjectId = useEqualityForObjectId;
return this;
}
/**
* Sets whether object members are serialized when their value is null.
* This has no impact on array elements. The default is true.
*/
public Builder serializeNulls( boolean serializeNulls ) {
this.serializeNulls = serializeNulls;
return this;
}
/**
* Determines whether {@link java.util.Date} and {@link java.sql.Timestamp} values are to be serialized as numeric timestamps
* (true; the default), or as textual representation.
* If textual representation is used, the actual format is
* {@link com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat#ISO_8601}
* Option is enabled by default.
*/
public Builder writeDatesAsTimestamps( boolean writeDatesAsTimestamps ) {
this.writeDatesAsTimestamps = writeDatesAsTimestamps;
return this;
}
/**
* Feature that determines whether {@link java.util.Date}s and {@link java.sql.Timestamp}s used as {@link java.util.Map} keys are
* serialized as timestamps or as textual values.
* If textual representation is used, the actual format is
* {@link com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat#ISO_8601}
* Option is disabled by default.
*/
public Builder writeDateKeysAsTimestamps( boolean writeDateKeysAsTimestamps ) {
this.writeDateKeysAsTimestamps = writeDateKeysAsTimestamps;
return this;
}
/**
* Feature that allows enabling (or disabling) indentation
* for the underlying writer.
* Feature is disabled by default.
*/
public Builder indent( boolean indent ) {
this.indent = indent;
return this;
}
/**
* Feature that can be enabled to make root value (usually JSON
* Object but can be any type) wrapped within a single property
* JSON object, where key as the "root name", as determined by
* annotation introspector or fallback (non-qualified
* class name).
* Feature is disabled by default.
*/
public Builder wrapRootValue( boolean wrapRootValue ) {
this.wrapRootValue = wrapRootValue;
return this;
}
/**
* Feature that determines how type char[]
is serialized:
* when enabled, will be serialized as an explict JSON array (with
* single-character Strings as values); when disabled, defaults to
* serializing them as Strings (which is more compact).
*
* Feature is disabled by default.
*
*/
public Builder writeCharArraysAsJsonArrays( boolean writeCharArraysAsJsonArrays ) {
this.writeCharArraysAsJsonArrays = writeCharArraysAsJsonArrays;
return this;
}
/**
* Feature that determines whether Map entries with null values are
* to be serialized (true) or not (false).
*
* Feature is enabled by default.
*
*/
public Builder writeNullMapValues( boolean writeNullMapValues ) {
this.writeNullMapValues = writeNullMapValues;
return this;
}
/**
* Feature that determines whether Container properties (POJO properties
* with declared value of Collection or array; i.e. things that produce JSON
* arrays) that are empty (have no elements)
* will be serialized as empty JSON arrays (true), or suppressed from output (false).
*
* Note that this does not change behavior of {@link java.util.Map}s, or
* "Collection-like" types.
*
*
* Feature is enabled by default.
*
*/
public Builder writeEmptyJsonArrays( boolean writeEmptyJsonArrays ) {
this.writeEmptyJsonArrays = writeEmptyJsonArrays;
return this;
}
/**
* Feature that determines whether {@link java.util.Map} entries are first
* sorted by key before serialization or not: if enabled, additional sorting
* step is performed if necessary (not necessary for {@link java.util.SortedMap}s),
* if disabled, no additional sorting is needed.
*
* Feature is disabled by default.
*
*/
public Builder orderMapEntriesByKeys( boolean orderMapEntriesByKeys ) {
this.orderMapEntriesByKeys = orderMapEntriesByKeys;
return this;
}
/**
* Feature added for interoperability, to work with oddities of
* so-called "BadgerFish" convention.
* Feature determines handling of single element {@link java.util.Collection}s
* and arrays: if enabled, {@link java.util.Collection}s and arrays that contain exactly
* one element will be serialized as if that element itself was serialized.
*
*
* When enabled, a POJO with array that normally looks like this:
*
* { "arrayProperty" : [ 1 ] }
*
* will instead be serialized as
*
* { "arrayProperty" : 1 }
*
*
* Note that this feature is counterpart to {@link JsonDeserializationContext.Builder#acceptSingleValueAsArray(boolean)}
* (that is, usually both are enabled, or neither is).
*
* Feature is disabled by default, so that no special handling is done.
*/
public Builder writeSingleElemArraysUnwrapped( boolean writeSingleElemArraysUnwrapped ) {
this.writeSingleElemArraysUnwrapped = writeSingleElemArraysUnwrapped;
return this;
}
/**
* Feature that determines whether gwt-jackson code should catch
* and wrap {@link RuntimeException}s (but never {@link Error}s!)
* to add additional information about
* location (within input) of problem or not. If enabled,
* exceptions will be caught and re-thrown; this can be
* convenient both in that all exceptions will be checked and
* declared, and so there is more contextual information.
* However, sometimes calling application may just want "raw"
* unchecked exceptions passed as is.
*
*
* Feature is enabled by default.
*/
public Builder wrapExceptions( boolean wrapExceptions ) {
this.wrapExceptions = wrapExceptions;
return this;
}
public final JsonSerializationContext build() {
return new JsonSerializationContext( useEqualityForObjectId, serializeNulls, writeDatesAsTimestamps,
writeDateKeysAsTimestamps, indent, wrapRootValue, writeCharArraysAsJsonArrays, writeNullMapValues,
writeEmptyJsonArrays, orderMapEntriesByKeys, writeSingleElemArraysUnwrapped, wrapExceptions );
}
}
public static class DefaultBuilder extends Builder {
private DefaultBuilder() { }
}
public static Builder builder() {
return GWT.create( Builder.class );
}
private static final Logger logger = Logger.getLogger( "JsonSerialization" );
private Map