From 648335b546f604bdf876c66126174c803644bbcf Mon Sep 17 00:00:00 2001 From: leventov Date: Mon, 23 Nov 2015 19:26:13 +0300 Subject: [PATCH] Add CharSequenceConverter --- .../map/ChroncileMapJsonSerializer.java | 6 +-- .../converters/CharSequenceConverter.java | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/openhft/xstream/converters/CharSequenceConverter.java diff --git a/src/main/java/net/openhft/chronicle/map/ChroncileMapJsonSerializer.java b/src/main/java/net/openhft/chronicle/map/ChroncileMapJsonSerializer.java index 4a9206835..9194d5be5 100644 --- a/src/main/java/net/openhft/chronicle/map/ChroncileMapJsonSerializer.java +++ b/src/main/java/net/openhft/chronicle/map/ChroncileMapJsonSerializer.java @@ -18,10 +18,7 @@ package net.openhft.chronicle.map; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.converters.Converter; import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver; -import net.openhft.xstream.converters.ByteBufferConverter; -import net.openhft.xstream.converters.StringBuilderConverter; -import net.openhft.xstream.converters.ValueConverter; -import net.openhft.xstream.converters.VanillaChronicleMapConverter; +import net.openhft.xstream.converters.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,6 +68,7 @@ class JsonSerializer { xstream.registerConverter(new ByteBufferConverter()); xstream.registerConverter(new ValueConverter()); xstream.registerConverter(new StringBuilderConverter()); + xstream.registerConverter(new CharSequenceConverter()); for (Object c : jsonConverters) { if (c instanceof Converter) { diff --git a/src/main/java/net/openhft/xstream/converters/CharSequenceConverter.java b/src/main/java/net/openhft/xstream/converters/CharSequenceConverter.java new file mode 100644 index 000000000..a2cf74c58 --- /dev/null +++ b/src/main/java/net/openhft/xstream/converters/CharSequenceConverter.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2015 higherfrequencytrading.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package net.openhft.xstream.converters; + +import com.thoughtworks.xstream.converters.Converter; +import com.thoughtworks.xstream.converters.MarshallingContext; +import com.thoughtworks.xstream.converters.UnmarshallingContext; +import com.thoughtworks.xstream.io.HierarchicalStreamReader; +import com.thoughtworks.xstream.io.HierarchicalStreamWriter; +import net.openhft.chronicle.hash.impl.util.CharSequences; + +public class CharSequenceConverter implements Converter { + + @Override + public void marshal( + Object source, HierarchicalStreamWriter writer, MarshallingContext context) { + writer.setValue(source.toString()); + } + + @Override + public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { + if (context.getRequiredType() == StringBuilder.class) { + return new StringBuilder(reader.getValue()); + } else { + return reader.getValue(); + } + } + + @Override + public boolean canConvert(Class type) { + return CharSequence.class.isAssignableFrom(type); + } +}