Skip to content

Commit

Permalink
Improve serialization performance in some cases
Browse files Browse the repository at this point in the history
When we can, we don't escape string name/value.
We could also do it on bean property name by escaping the string during the compilation
  • Loading branch information
nmorel committed Jan 13, 2015
1 parent 56ffffd commit 51ca927
Show file tree
Hide file tree
Showing 19 changed files with 325 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ public void doSerialize( JsonWriter writer, @Nonnull M multimap, JsonSerializati
if ( !multimap.isEmpty() ) {

for ( Entry<K, Collection<V>> entry : multimap.asMap().entrySet() ) {
writer.name( keySerializer.serialize( entry.getKey(), ctx ) );
String name = keySerializer.serialize( entry.getKey(), ctx );
if ( keySerializer.mustBeEscaped( ctx ) ) {
writer.name( name );
} else {
writer.unescapeName( name );
}
writer.beginArray();
for ( V value : entry.getValue() ) {
valueSerializer.serialize( writer, value, ctx, params );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ protected void doSerialize( JsonWriter writer, @Nonnull Date value, JsonSerializ
JsonSerializerParameters params ) {
if ( params.getShape().isNumeric() || ctx.isWriteDatesAsTimestamps() ) {
writer.value( value.getTime() );
} else if (null == params.getPattern()) {
writer.unescapeValue( DateFormat.format( value ) );
} else {
writer.value( DateFormat.format( params.getPattern(), value ) );
}
Expand All @@ -80,7 +82,7 @@ private SqlDateJsonSerializer() { }
@Override
protected void doSerialize( JsonWriter writer, @Nonnull java.sql.Date value, JsonSerializationContext ctx,
JsonSerializerParameters params ) {
writer.value( value.toString() );
writer.unescapeValue( value.toString() );
}
}

Expand All @@ -103,7 +105,7 @@ private SqlTimeJsonSerializer() { }
@Override
protected void doSerialize( JsonWriter writer, @Nonnull Time value, JsonSerializationContext ctx,
JsonSerializerParameters params ) {
writer.value( value.toString() );
writer.unescapeValue( value.toString() );
}
}

Expand All @@ -128,6 +130,8 @@ protected void doSerialize( JsonWriter writer, @Nonnull Timestamp value, JsonSer
JsonSerializerParameters params ) {
if ( params.getShape().isNumeric() || ctx.isWriteDatesAsTimestamps() ) {
writer.value( value.getTime() );
} else if (null == params.getPattern()) {
writer.unescapeValue( DateFormat.format( value ) );
} else {
writer.value( DateFormat.format( params.getPattern(), value ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ private EnumJsonSerializer() { }

@Override
public void doSerialize( JsonWriter writer, @Nonnull E value, JsonSerializationContext ctx, JsonSerializerParameters params ) {
writer.value( value.name() );
writer.unescapeValue( value.name() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ private UUIDJsonSerializer() { }

@Override
public void doSerialize( JsonWriter writer, @Nonnull UUID value, JsonSerializationContext ctx, JsonSerializerParameters params ) {
writer.value( value.toString() );
writer.unescapeValue( value.toString() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public void doSerialize( JsonWriter writer, @Nonnull byte[] values, JsonSerializ
return;
}

writer.value( Base64Utils.toBase64( values ) );
writer.unescapeValue( Base64Utils.toBase64( values ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void doSerialize( JsonWriter writer, @Nonnull byte[][] values, JsonSerial

writer.beginArray();
for ( byte[] array : values ) {
writer.value( Base64Utils.toBase64( array ) );
writer.unescapeValue( Base64Utils.toBase64( array ) );
}
writer.endArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,25 @@ public void serializeValues( JsonWriter writer, M values, JsonSerializationConte
if ( ctx.isWriteNullMapValues() ) {

for ( Entry<K, V> entry : map.entrySet() ) {
writer.name( keySerializer.serialize( entry.getKey(), ctx ) );
String name = keySerializer.serialize( entry.getKey(), ctx );
if ( keySerializer.mustBeEscaped( ctx ) ) {
writer.name( name );
} else {
writer.unescapeName( name );
}
valueSerializer.serialize( writer, entry.getValue(), ctx, params );
}

} else {

for ( Entry<K, V> entry : map.entrySet() ) {
if ( null != entry.getValue() ) {
writer.name( keySerializer.serialize( entry.getKey(), ctx ) );
String name = keySerializer.serialize( entry.getKey(), ctx );
if ( keySerializer.mustBeEscaped( ctx ) ) {
writer.name( name );
} else {
writer.unescapeName( name );
}
valueSerializer.serialize( writer, entry.getValue(), ctx, params );
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2015 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.ser.map.key;

import javax.annotation.Nonnull;

import com.github.nmorel.gwtjackson.client.JsonSerializationContext;

/**
* Default {@link KeySerializer} implementation for {@link Boolean}.
*
* @author Nicolas Morel
*/
public final class BooleanKeySerializer extends KeySerializer<Boolean> {

private static final BooleanKeySerializer INSTANCE = new BooleanKeySerializer();

/**
* @return an instance of {@link BooleanKeySerializer}
*/
@SuppressWarnings( "unchecked" )
public static BooleanKeySerializer getInstance() {
return INSTANCE;
}

private BooleanKeySerializer() { }

@Override
public boolean mustBeEscaped( JsonSerializationContext ctx ) {
return false;
}

@Override
protected String doSerialize( @Nonnull Boolean value, JsonSerializationContext ctx ) {
return value.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public static DateKeySerializer getInstance() {

private DateKeySerializer() { }

@Override
public boolean mustBeEscaped( JsonSerializationContext ctx ) {
return false;
}

@Override
protected String doSerialize( @Nonnull Date value, JsonSerializationContext ctx ) {
if ( ctx.isWriteDateKeysAsTimestamps() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ public final class EnumKeySerializer<E extends Enum<E>> extends KeySerializer<E>
/**
* @return an instance of {@link EnumKeySerializer}
*/
@SuppressWarnings("unchecked")
@SuppressWarnings( "unchecked" )
public static <S extends EnumKeySerializer<?>> S getInstance() {
return (S) INSTANCE;
}

private EnumKeySerializer() { }

@Override
public boolean mustBeEscaped( JsonSerializationContext ctx ) {
return false;
}

@Override
protected String doSerialize( @Nonnull E value, JsonSerializationContext ctx ) {
return value.name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
*/
public abstract class KeySerializer<T> {

/**
* @param ctx Context for the full serialization process
*
* @return true if the serialized key must be escaped
*/
public boolean mustBeEscaped( JsonSerializationContext ctx ) {
return true;
}

/**
* Serializes an object into a {@link String} to use as map's key.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2015 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.ser.map.key;

import javax.annotation.Nonnull;

import com.github.nmorel.gwtjackson.client.JsonSerializationContext;

/**
* Default {@link KeySerializer} implementation for {@link Number}.
*
* @author Nicolas Morel
*/
public final class NumberKeySerializer extends KeySerializer<Number> {

private static final NumberKeySerializer INSTANCE = new NumberKeySerializer();

/**
* @return an instance of {@link NumberKeySerializer}
*/
@SuppressWarnings( "unchecked" )
public static NumberKeySerializer getInstance() {
return INSTANCE;
}

private NumberKeySerializer() { }

@Override
public boolean mustBeEscaped( JsonSerializationContext ctx ) {
return false;
}

@Override
protected String doSerialize( @Nonnull Number value, JsonSerializationContext ctx ) {
return value.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2015 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.ser.map.key;

import javax.annotation.Nonnull;

import java.util.UUID;

import com.github.nmorel.gwtjackson.client.JsonSerializationContext;

/**
* Default {@link KeySerializer} implementation for {@link UUID}.
*
* @author Nicolas Morel
*/
public final class UUIDKeySerializer extends KeySerializer<UUID> {

private static final UUIDKeySerializer INSTANCE = new UUIDKeySerializer();

/**
* @return an instance of {@link UUIDKeySerializer}
*/
@SuppressWarnings( "unchecked" )
public static UUIDKeySerializer getInstance() {
return INSTANCE;
}

private UUIDKeySerializer() { }

@Override
public boolean mustBeEscaped( JsonSerializationContext ctx ) {
return false;
}

@Override
protected String doSerialize( @Nonnull UUID value, JsonSerializationContext ctx ) {
return value.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public interface JsonWriter {
*/
JsonWriter name( String name );

/**
* Encodes the property name without escaping it.
*
* @param name the name of the forthcoming value. May not be null.
* @return this writer.
*/
JsonWriter unescapeName( String name );

/**
* Encodes {@code value}.
*
Expand All @@ -96,6 +104,14 @@ public interface JsonWriter {
*/
JsonWriter value( String value );

/**
* Encodes {@code value} without escaping it.
*
* @param value the literal string value, or null to encode a null literal.
* @return this writer.
*/
JsonWriter unescapeValue( String value );

/**
* Encodes {@code null}.
*
Expand Down
Loading

0 comments on commit 51ca927

Please sign in to comment.