Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2023 See AUTHORS file.
*
* 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.tommyettinger.ds.interop;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.RandomXS128;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonValue;
import com.badlogic.gdx.utils.JsonWriter;
import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.badlogic.gdx.utils.reflect.ReflectionException;
import com.github.tommyettinger.digital.*;
import com.github.tommyettinger.digital.Interpolations.Interpolator;
import com.github.tommyettinger.ds.*;
import com.github.tommyettinger.ds.support.util.*;
import com.github.tommyettinger.random.*;
import com.github.tommyettinger.random.distribution.*;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
@SuppressWarnings("rawtypes")
public final class JsonSupport {
private JsonSupport() {
}
@NonNull
private static Base BASE = Base.BASE10;
private static boolean LEGIBLE_FLOATS = true;
private static boolean ADD_CLASS_TAGS = true;
/**
* Registers JDKGDXDS' classes with the given Json object, allowing it to read and write JDKGDXDS types.
*
* @param json a libGDX Json object that will have serializers registered for all JDKGDXDS types.
*/
public static void registerAll(@NonNull Json json) {
registerObjectList(json);
registerIntList(json);
registerLongList(json);
registerFloatList(json);
registerByteList(json);
registerShortList(json);
registerCharList(json);
registerDoubleList(json);
registerBooleanList(json);
registerObjectBag(json);
registerIntBag(json);
registerLongBag(json);
registerFloatBag(json);
registerByteBag(json);
registerShortBag(json);
registerCharBag(json);
registerDoubleBag(json);
registerBooleanBag(json);
registerObjectDeque(json);
registerLongDeque(json);
registerIntDeque(json);
registerShortDeque(json);
registerByteDeque(json);
registerDoubleDeque(json);
registerFloatDeque(json);
registerCharDeque(json);
registerBooleanDeque(json);
registerObjectSet(json);
registerObjectOrderedSet(json);
registerIntSet(json);
registerIntOrderedSet(json);
registerLongSet(json);
registerLongOrderedSet(json);
registerOffsetBitSet(json);
registerEnumSet(json);
registerObjectObjectMap(json);
registerObjectObjectOrderedMap(json);
registerObjectIntMap(json);
registerObjectIntOrderedMap(json);
registerObjectLongMap(json);
registerObjectLongOrderedMap(json);
registerObjectFloatMap(json);
registerObjectFloatOrderedMap(json);
registerIntObjectMap(json);
registerIntObjectOrderedMap(json);
registerIntIntMap(json);
registerIntIntOrderedMap(json);
registerIntLongMap(json);
registerIntLongOrderedMap(json);
registerIntFloatMap(json);
registerIntFloatOrderedMap(json);
registerLongObjectMap(json);
registerLongObjectOrderedMap(json);
registerLongIntMap(json);
registerLongIntOrderedMap(json);
registerLongLongMap(json);
registerLongLongOrderedMap(json);
registerLongFloatMap(json);
registerLongFloatOrderedMap(json);
registerEnumMap(json);
registerCaseInsensitiveSet(json);
registerCaseInsensitiveOrderedSet(json);
registerCaseInsensitiveMap(json);
registerCaseInsensitiveOrderedMap(json);
registerFilteredStringSet(json);
registerFilteredStringOrderedSet(json);
registerFilteredStringMap(json);
registerFilteredStringOrderedMap(json);
registerNumberedSet(json);
registerBinaryHeap(json);
// from digital.
registerBase(json);
registerHasher(json);
registerAlternateRandom(json);
registerInterpolator(json);
// from juniper. these register many others.
registerDistributedRandom(json);
registerInterpolatedRandom(json);
// from libGDX.
registerRandomXS128(json);
// from the JDK.
registerClass(json);
}
/**
* Gets the numeral system, also called radix or base, used by some methods here to encode numbers.
* If it hasn't been changed, the default this uses is {@link Base#BASE10}, because it is the most human-readable.
* @return the Base system this uses, which is always non-null.
*/
public static Base getNumeralBase() {
return BASE;
}
/**
* Sets the numeral system, also called radix or base, used by some methods here to encode numbers.
* This is most likely to be used with {@link Base#scrambledBase(Random)} to obfuscate specific numbers,
* such as random seeds, put into readable JSON files. The methods this affects are mostly related to registering
* {@link EnhancedRandom} and its implementations, {@link RandomXS128}, and {@link AtomicLong}. If this hasn't been
* called, the default this uses is {@link Base#BASE10}, because it is the most human-readable.
* @param base a non-null Base system
*/
public static void setNumeralBase(Base base){
if(base != null)
BASE = base;
}
/**
* Gets the status of whether this will write float and double items using {@link Base#general(float)} (when true)
* or {@link Base#signed(int)} (when false). Sometimes one option will produce smaller output, and sometimes the
* other will. If this is false, then there is a maximum length a float or double will use (in chars). If this is
* true, then floats and doubles will always print in base-10, and only use scientific notation for very large or
* very small numbers (determined by distance from 0.0), but the maximum length is larger.
* @return true if this is writing floats in a human-readable way, or false if writing floats in a compact way
*/
public static boolean areFloatsLegible() {
return LEGIBLE_FLOATS;
}
/**
* Sets the status of whether this will write float and double items using {@link Base#general(float)} (when true)
* or {@link Base#signed(int)} (when false). Sometimes one option will produce smaller output, and sometimes the
* other will. If this is false, then there is a maximum length a float or double will use (in chars). If this is
* true, then floats and doubles will always print in base-10, and only use scientific notation for very large or
* very small numbers (determined by distance from 0.0), but the maximum length is larger.
* @param legibleFloats true to write floats in a human-readable way, or false to write floats in a compact way
*/
public static void setFloatsLegible(boolean legibleFloats) {
LEGIBLE_FLOATS = legibleFloats;
}
/**
* Gets the status of whether this will add short class tags when registering classes. If true (the default), this
* will use very short class tags. If false, this will use the normal Json behavior of package-qualified class
* names, which can be quite long.
* @return true if this is currently set to add class tags when registering classes, or false otherwise
*/
public static boolean isAddClassTags() {
return ADD_CLASS_TAGS;
}
/**
* If true, this will call {@link Json#addClassTag(String, Class)} to register a very short name when
* registering most classes. If false, registering classes will not register class tags. Using short class tags can
* impede readability, but if users are expected to either not see raw JSON files or not care about {@code class}
* entries in them, then using short tags saves a lot of file size. This is especially useful for games that
* transmit JSON over a network. The default for this, if not yet called, is true (it will use short tags).
* @param addClassTags if true, this will use short class tags instead of long names with packages for classes
*/
public static void setAddClassTags(boolean addClassTags) {
ADD_CLASS_TAGS = addClassTags;
}
private static String str(float data) {
return LEGIBLE_FLOATS ? BASE.general(data) : BASE.signed(data);
}
private static String str(double data) {
return LEGIBLE_FLOATS ? BASE.general(data) : BASE.signed(data);
}
private static StringBuilder append(StringBuilder sb, float data) {
return LEGIBLE_FLOATS ? BASE.appendGeneral(sb, data) : BASE.appendSigned(sb, data);
}
private static StringBuilder append(StringBuilder sb, double data) {
return LEGIBLE_FLOATS ? BASE.appendGeneral(sb, data) : BASE.appendSigned(sb, data);
}
private static String join(float[] data) {
return LEGIBLE_FLOATS ? BASE.join(" ", data) : BASE.joinExact(" ", data);
}
private static String join(double[] data) {
return LEGIBLE_FLOATS ? BASE.join(" ", data) : BASE.joinExact(" ", data);
}
private static StringBuilder appendJoined(StringBuilder sb, float[] data) {
return LEGIBLE_FLOATS ? BASE.appendJoined(sb, " ", data) : BASE.appendJoinedExact(sb, " ", data);
}
private static StringBuilder appendJoined(StringBuilder sb, double[] data) {
return LEGIBLE_FLOATS ? BASE.appendJoined(sb, " ", data) : BASE.appendJoinedExact(sb, " ", data);
}
private static String join(float[] data, int start, int length) {
return LEGIBLE_FLOATS ? BASE.join(" ", data, start, length) : BASE.joinExact(" ", data, start, length);
}
private static String join(double[] data, int start, int length) {
return LEGIBLE_FLOATS ? BASE.join(" ", data, start, length) : BASE.joinExact(" ", data, start, length);
}
private static StringBuilder appendJoined(StringBuilder sb, float[] data, int start, int length) {
return LEGIBLE_FLOATS ? BASE.appendJoined(sb, " ", data, start, length) : BASE.appendJoinedExact(sb, " ", data, start, length);
}
private static StringBuilder appendJoined(StringBuilder sb, double[] data, int start, int length) {
return LEGIBLE_FLOATS ? BASE.appendJoined(sb, " ", data, start, length) : BASE.appendJoinedExact(sb, " ", data, start, length);
}
private static float floatRead(String data) {
return LEGIBLE_FLOATS ? BASE.readFloat(data) : BASE.readFloatExact(data);
}
private static double doubleRead(String data) {
return LEGIBLE_FLOATS ? BASE.readDouble(data) : BASE.readDoubleExact(data);
}
private static float[] floatSplit(String data) {
return LEGIBLE_FLOATS ? BASE.floatSplit(data, " ") : BASE.floatSplitExact(data, " ");
}
private static double[] doubleSplit(String data) {
return LEGIBLE_FLOATS ? BASE.doubleSplit(data, " ") : BASE.doubleSplitExact(data, " ");
}
/**
* Registers ObjectList with the given Json object, so ObjectList can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerObjectList(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("oL", ObjectList.class);
json.setSerializer(ObjectList.class, new Json.Serializer() {
@Override
public void write(Json json, ObjectList object, Class knownType) {
json.writeObjectStart(ObjectList.class, knownType);
json.writeArrayStart("items");
for (Object o : object) {
json.writeValue(o, null);
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public ObjectList> read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull()) return null;
ObjectList> data = new ObjectList<>(jsonData.size);
for (JsonValue value = jsonData.child; value != null; value = value.next) {
data.add(json.readValue(null, value));
}
return data;
}
});
}
/**
* Registers IntList with the given Json object, so IntList can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerIntList(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("iL", IntList.class);
json.setSerializer(IntList.class, new Json.Serializer() {
@Override
public void write(Json json, IntList object, Class knownType) {
json.writeObjectStart(IntList.class, knownType);
json.writeValue("items", BASE.join(" ", object.items, 0, object.size()));
json.writeObjectEnd();
}
@Override
public IntList read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return IntList.with(BASE.intSplit(jsonData.asString(), " "));
}
});
}
/**
* Registers LongList with the given Json object, so LongList can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerLongList(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("lL", LongList.class);
json.setSerializer(LongList.class, new Json.Serializer() {
@Override
public void write(Json json, LongList object, Class knownType) {
json.writeObjectStart(LongList.class, knownType);
json.writeValue("items", BASE.join(" ", object.items, 0, object.size()));
json.writeObjectEnd();
}
@Override
public LongList read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return LongList.with(BASE.longSplit(jsonData.asString(), " "));
}
});
}
/**
* Registers FloatList with the given Json object, so FloatList can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerFloatList(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("fL", FloatList.class);
json.setSerializer(FloatList.class, new Json.Serializer() {
@Override
public void write(Json json, FloatList object, Class knownType) {
json.writeObjectStart(FloatList.class, knownType);
json.writeValue("items", join(object.items, 0, object.size()));
json.writeObjectEnd();
}
@Override
public FloatList read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return FloatList.with(floatSplit(jsonData.asString()));
}
});
}
/**
* Registers ByteList with the given Json object, so ByteList can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerByteList(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("bL", ByteList.class);
json.setSerializer(ByteList.class, new Json.Serializer() {
@Override
public void write(Json json, ByteList object, Class knownType) {
json.writeObjectStart(ByteList.class, knownType);
json.writeValue("items", BASE.join(" ", object.items, 0, object.size()));
json.writeObjectEnd();
}
@Override
public ByteList read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return ByteList.with(BASE.byteSplit(jsonData.asString(), " "));
}
});
}
/**
* Registers ShortList with the given Json object, so ShortList can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerShortList(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("sL", ShortList.class);
json.setSerializer(ShortList.class, new Json.Serializer() {
@Override
public void write(Json json, ShortList object, Class knownType) {
json.writeObjectStart(ShortList.class, knownType);
json.writeValue("items", BASE.join(" ", object.items, 0, object.size()));
json.writeObjectEnd();
}
@Override
public ShortList read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return ShortList.with(BASE.shortSplit(jsonData.asString(), " "));
}
});
}
/**
* Registers CharList with the given Json object, so CharList can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerCharList(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("cL", CharList.class);
json.setSerializer(CharList.class, new Json.Serializer() {
@Override
public void write(Json json, CharList object, Class knownType) {
json.writeObjectStart(CharList.class, knownType);
json.writeValue("data", object.toDenseString(), String.class);
json.writeObjectEnd();
}
@Override
public CharList read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("data")) == null) return null;
return CharList.with(jsonData.asString().toCharArray());
}
});
}
/**
* Registers DoubleList with the given Json object, so DoubleList can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerDoubleList(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("dL", DoubleList.class);
json.setSerializer(DoubleList.class, new Json.Serializer() {
@Override
public void write(Json json, DoubleList object, Class knownType) {
json.writeObjectStart(DoubleList.class, knownType);
json.writeValue("items", join(object.items, 0, object.size()));
json.writeObjectEnd();
}
@Override
public DoubleList read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return DoubleList.with(doubleSplit(jsonData.asString()));
}
});
}
/**
* Registers BooleanList with the given Json object, so BooleanList can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerBooleanList(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("tL", BooleanList.class); // t for truth; represents boolean
json.setSerializer(BooleanList.class, new Json.Serializer() {
@Override
public void write(Json json, BooleanList object, Class knownType) {
json.writeObjectStart(BooleanList.class, knownType);
json.writeValue("items", TextTools.joinDense(object.items, 0, object.size()));
json.writeObjectEnd();
}
@Override
public BooleanList read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return BooleanList.with(TextTools.booleanSplitDense(jsonData.asString()));
}
});
}
/**
* Registers ObjectBag with the given Json object, so ObjectBag can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerObjectBag(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("oB", ObjectBag.class);
json.setSerializer(ObjectBag.class, new Json.Serializer() {
@Override
public void write(Json json, ObjectBag object, Class knownType) {
json.writeObjectStart(ObjectBag.class, knownType);
json.writeArrayStart("items");
for (Object o : object) {
json.writeValue(o, null);
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public ObjectBag> read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull()) return null;
ObjectBag> data = new ObjectBag<>(jsonData.size);
for (JsonValue value = jsonData.child; value != null; value = value.next) {
data.add(json.readValue(null, value));
}
return data;
}
});
}
/**
* Registers IntBag with the given Json object, so IntBag can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerIntBag(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("iB", IntBag.class);
json.setSerializer(IntBag.class, new Json.Serializer() {
@Override
public void write(Json json, IntBag object, Class knownType) {
json.writeObjectStart(IntBag.class, knownType);
json.writeArrayStart("items");
IntIterator it = object.iterator();
while (it.hasNext()) {
json.writeValue(it.nextInt());
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public IntBag read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return IntBag.with(jsonData.asIntArray());
}
});
}
/**
* Registers LongBag with the given Json object, so LongBag can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerLongBag(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("lB", LongBag.class);
json.setSerializer(LongBag.class, new Json.Serializer() {
@Override
public void write(Json json, LongBag object, Class knownType) {
json.writeObjectStart(LongBag.class, knownType);
json.writeArrayStart("items");
LongIterator it = object.iterator();
while (it.hasNext()) {
json.writeValue(it.nextLong());
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public LongBag read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return LongBag.with(jsonData.asLongArray());
}
});
}
/**
* Registers FloatBag with the given Json object, so FloatBag can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerFloatBag(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("fB", FloatBag.class);
json.setSerializer(FloatBag.class, new Json.Serializer() {
@Override
public void write(Json json, FloatBag object, Class knownType) {
json.writeObjectStart(FloatBag.class, knownType);
json.writeValue("items", join(object.items, 0, object.size()));
json.writeObjectEnd();
}
@Override
public FloatBag read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return FloatBag.with(floatSplit(jsonData.asString()));
}
});
}
/**
* Registers ByteBag with the given Json object, so ByteBag can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerByteBag(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("bB", ByteBag.class);
json.setSerializer(ByteBag.class, new Json.Serializer() {
@Override
public void write(Json json, ByteBag object, Class knownType) {
json.writeObjectStart(ByteBag.class, knownType);
json.writeArrayStart("items");
ByteIterator it = object.iterator();
while (it.hasNext()) {
json.writeValue(it.nextByte());
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public ByteBag read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return ByteBag.with(jsonData.asByteArray());
}
});
}
/**
* Registers ShortBag with the given Json object, so ShortBag can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerShortBag(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("sB", ShortBag.class);
json.setSerializer(ShortBag.class, new Json.Serializer() {
@Override
public void write(Json json, ShortBag object, Class knownType) {
json.writeObjectStart(ShortBag.class, knownType);
json.writeArrayStart("items");
ShortIterator it = object.iterator();
while (it.hasNext()) {
json.writeValue(it.nextShort());
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public ShortBag read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return ShortBag.with(jsonData.asShortArray());
}
});
}
/**
* Registers CharBag with the given Json object, so CharBag can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerCharBag(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("cB", CharBag.class);
json.setSerializer(CharBag.class, new Json.Serializer() {
@Override
public void write(Json json, CharBag object, Class knownType) {
json.writeObjectStart(CharBag.class, knownType);
json.writeArrayStart("items");
CharIterator it = object.iterator();
while (it.hasNext()) {
json.writeValue(it.nextChar());
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public CharBag read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return CharBag.with(jsonData.asCharArray());
}
});
}
/**
* Registers DoubleBag with the given Json object, so DoubleBag can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerDoubleBag(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("dB", DoubleBag.class);
json.setSerializer(DoubleBag.class, new Json.Serializer() {
@Override
public void write(Json json, DoubleBag object, Class knownType) {
json.writeObjectStart(DoubleBag.class, knownType);
json.writeValue("items", join(object.items, 0, object.size()));
json.writeObjectEnd();
}
@Override
public DoubleBag read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return DoubleBag.with(doubleSplit(jsonData.asString()));
}
});
}
/**
* Registers BooleanBag with the given Json object, so BooleanBag can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerBooleanBag(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("tB", BooleanBag.class); // t for truth; represents boolean
json.setSerializer(BooleanBag.class, new Json.Serializer() {
@Override
public void write(Json json, BooleanBag object, Class knownType) {
json.writeObjectStart(BooleanBag.class, knownType);
json.writeValue("items", TextTools.joinDense(object.items, 0, object.size()));
json.writeObjectEnd();
}
@Override
public BooleanBag read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return BooleanBag.with(TextTools.booleanSplitDense(jsonData.asString()));
}
});
}
/**
* Registers ObjectDeque with the given Json object, so ObjectDeque can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerObjectDeque(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("oQ", ObjectDeque.class);
json.setSerializer(ObjectDeque.class, new Json.Serializer() {
@Override
public void write(Json json, ObjectDeque object, Class knownType) {
json.writeObjectStart(ObjectDeque.class, knownType);
json.writeArrayStart("items");
for (Object o : object) {
json.writeValue(o, null);
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public ObjectDeque> read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull()) return null;
ObjectDeque> data = new ObjectDeque<>(jsonData.size);
for (JsonValue value = jsonData.child; value != null; value = value.next) {
data.add(json.readValue(null, value));
}
return data;
}
});
}
/**
* Registers LongDeque with the given Json object, so LongDeque can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerLongDeque(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("lQ", LongDeque.class);
json.setSerializer(LongDeque.class, new Json.Serializer() {
@Override
public void write(Json json, LongDeque object, Class knownType) {
json.writeObjectStart(LongDeque.class, knownType);
json.writeArrayStart("items");
LongIterator it = object.iterator();
while (it.hasNext()) {
json.writeValue(it.nextLong());
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public LongDeque read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return LongDeque.with(jsonData.asLongArray());
}
});
}
/**
* Registers IntDeque with the given Json object, so IntDeque can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerIntDeque(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("iQ", IntDeque.class);
json.setSerializer(IntDeque.class, new Json.Serializer() {
@Override
public void write(Json json, IntDeque object, Class knownType) {
json.writeObjectStart(IntDeque.class, knownType);
json.writeArrayStart("items");
IntIterator it = object.iterator();
while (it.hasNext()) {
json.writeValue(it.nextInt());
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public IntDeque read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return IntDeque.with(jsonData.asIntArray());
}
});
}
/**
* Registers CharDeque with the given Json object, so CharDeque can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerCharDeque(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("cQ", CharDeque.class);
json.setSerializer(CharDeque.class, new Json.Serializer() {
@Override
public void write(Json json, CharDeque object, Class knownType) {
json.writeObjectStart(CharDeque.class, knownType);
json.writeArrayStart("items");
CharIterator it = object.iterator();
while (it.hasNext()) {
json.writeValue(it.nextChar());
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public CharDeque read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return CharDeque.with(jsonData.asCharArray());
}
});
}
/**
* Registers ShortDeque with the given Json object, so ShortDeque can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerShortDeque(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("sQ", ShortDeque.class);
json.setSerializer(ShortDeque.class, new Json.Serializer() {
@Override
public void write(Json json, ShortDeque object, Class knownType) {
json.writeObjectStart(ShortDeque.class, knownType);
json.writeArrayStart("items");
ShortIterator it = object.iterator();
while (it.hasNext()) {
json.writeValue(it.nextShort());
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public ShortDeque read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return ShortDeque.with(jsonData.asShortArray());
}
});
}
/**
* Registers ByteDeque with the given Json object, so ByteDeque can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerByteDeque(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("bQ", ByteDeque.class);
json.setSerializer(ByteDeque.class, new Json.Serializer() {
@Override
public void write(Json json, ByteDeque object, Class knownType) {
json.writeObjectStart(ByteDeque.class, knownType);
json.writeArrayStart("items");
ByteIterator it = object.iterator();
while (it.hasNext()) {
json.writeValue(it.nextByte());
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public ByteDeque read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return ByteDeque.with(jsonData.asByteArray());
}
});
}
/**
* Registers FloatDeque with the given Json object, so FloatDeque can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerFloatDeque(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("fQ", FloatDeque.class);
json.setSerializer(FloatDeque.class, new Json.Serializer() {
@Override
public void write(Json json, FloatDeque object, Class knownType) {
json.writeObjectStart(FloatDeque.class, knownType);
StringBuilder sb = new StringBuilder(object.size());
FloatIterator it = object.iterator();
while (it.hasNext()) {
sb.append(' ');
append(sb, it.nextFloat());
}
json.writeValue("items", sb.substring(1));
json.writeObjectEnd();
}
@Override
public FloatDeque read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return FloatDeque.with(floatSplit(jsonData.asString()));
}
});
}
/**
* Registers DoubleDeque with the given Json object, so DoubleDeque can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerDoubleDeque(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("dQ", DoubleDeque.class);
json.setSerializer(DoubleDeque.class, new Json.Serializer() {
@Override
public void write(Json json, DoubleDeque object, Class knownType) {
json.writeObjectStart(DoubleDeque.class, knownType);
StringBuilder sb = new StringBuilder(object.size());
DoubleIterator it = object.iterator();
while (it.hasNext()) {
sb.append(' ');
append(sb, it.nextDouble());
}
json.writeValue("items", sb.substring(1));
json.writeObjectEnd();
}
@Override
public DoubleDeque read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return DoubleDeque.with(doubleSplit(jsonData.asString()));
}
});
}
/**
* Registers BooleanDeque with the given Json object, so BooleanDeque can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerBooleanDeque(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("tQ", ObjectDeque.class); // t for truth (boolean)
json.setSerializer(BooleanDeque.class, new Json.Serializer() {
@Override
public void write(Json json, BooleanDeque object, Class knownType) {
json.writeObjectStart(BooleanDeque.class, knownType);
StringBuilder sb = new StringBuilder(object.size());
BooleanIterator it = object.iterator();
while (it.hasNext()) {
sb.append(it.nextBoolean() ? '1' : '0');
}
json.writeValue("items", sb.toString());
json.writeObjectEnd();
}
@Override
public BooleanDeque read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return BooleanDeque.with(TextTools.booleanSplitDense(jsonData.asString()));
}
});
}
/**
* Registers EnumSet with the given Json object, so EnumSet can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerEnumSet(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("eS", EnumSet.class);
json.setSerializer(EnumSet.class, new Json.Serializer() {
@Override
public void write(Json json, EnumSet object, Class knownType) {
json.writeObjectStart(EnumSet.class, knownType);
json.writeArrayStart("items"); // This name is special.
for (Enum> o : object) {
json.writeValue(o, Enum.class);
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public EnumSet read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull()) return null;
EnumSet data = new EnumSet();
for (JsonValue value = jsonData.child; value != null; value = value.next) {
data.add(json.readValue(Enum.class, value));
}
return data;
}
});
}
/**
* Registers ObjectSet with the given Json object, so ObjectSet can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerObjectSet(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("oS", ObjectSet.class);
json.setSerializer(ObjectSet.class, new Json.Serializer() {
@Override
public void write(Json json, ObjectSet object, Class knownType) {
json.writeObjectStart(ObjectSet.class, knownType);
json.writeArrayStart("items"); // This name is special.
for (Object o : object) {
json.writeValue(o, null);
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public ObjectSet> read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull()) return null;
ObjectSet> data = new ObjectSet<>(jsonData.size);
for (JsonValue value = jsonData.child; value != null; value = value.next) {
data.add(json.readValue(null, value));
}
return data;
}
});
}
/**
* Registers ObjectOrderedSet with the given Json object, so ObjectOrderedSet can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerObjectOrderedSet(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("oOS", ObjectOrderedSet.class);
json.setSerializer(ObjectOrderedSet.class, new Json.Serializer() {
@Override
public void write(Json json, ObjectOrderedSet object, Class knownType) {
json.writeObjectStart(ObjectOrderedSet.class, knownType);
json.writeArrayStart("items"); // This name is special.
for (Object o : object) {
json.writeValue(o, null);
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public ObjectOrderedSet> read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull()) return null;
ObjectOrderedSet> data = new ObjectOrderedSet<>(jsonData.size);
for (JsonValue value = jsonData.child; value != null; value = value.next) {
data.add(json.readValue(null, value));
}
return data;
}
});
}
/**
* Registers IntSet with the given Json object, so IntSet can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerIntSet(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("iS", IntSet.class);
json.setSerializer(IntSet.class, new Json.Serializer() {
@Override
public void write(Json json, IntSet object, Class knownType) {
json.writeObjectStart(IntSet.class, knownType);
json.writeArrayStart("items");
IntIterator it = object.iterator();
while (it.hasNext()) {
json.writeValue(it.nextInt());
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public IntSet read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return IntSet.with(jsonData.asIntArray());
}
});
}
/**
* Registers IntOrderedSet with the given Json object, so IntOrderedSet can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerIntOrderedSet(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("iOS", IntOrderedSet.class);
json.setSerializer(IntOrderedSet.class, new Json.Serializer() {
@Override
public void write(Json json, IntOrderedSet object, Class knownType) {
json.writeObjectStart(IntOrderedSet.class, knownType);
json.writeArrayStart("items");
IntIterator it = object.iterator();
while (it.hasNext()) {
json.writeValue(it.nextInt());
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public IntOrderedSet read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return IntOrderedSet.with(jsonData.asIntArray());
}
});
}
/**
* Registers LongSet with the given Json object, so LongSet can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerLongSet(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("lS", LongSet.class);
json.setSerializer(LongSet.class, new Json.Serializer() {
@Override
public void write(Json json, LongSet object, Class knownType) {
json.writeObjectStart(LongSet.class, knownType);
json.writeArrayStart("items");
LongIterator it = object.iterator();
while (it.hasNext()) {
json.writeValue(it.nextLong());
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public LongSet read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return LongSet.with(jsonData.asLongArray());
}
});
}
/**
* Registers LongOrderedSet with the given Json object, so LongOrderedSet can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerLongOrderedSet(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("lOS", LongOrderedSet.class);
json.setSerializer(LongOrderedSet.class, new Json.Serializer() {
@Override
public void write(Json json, LongOrderedSet object, Class knownType) {
json.writeObjectStart(LongOrderedSet.class, knownType);
json.writeArrayStart("items");
LongIterator it = object.iterator();
while (it.hasNext()) {
json.writeValue(it.nextLong());
}
json.writeArrayEnd();
json.writeObjectEnd();
}
@Override
public LongOrderedSet read(Json json, JsonValue jsonData, Class type) {
if (jsonData == null || jsonData.isNull() || (jsonData = jsonData.get("items")) == null) return null;
return LongOrderedSet.with(jsonData.asLongArray());
}
});
}
/**
* Registers EnumMap with the given Json object, so EnumMap can be written to and read from JSON.
*
* @param json a libGDX Json object that will have a serializer registered
*/
public static void registerEnumMap(@NonNull Json json) {
if(ADD_CLASS_TAGS) json.addClassTag("eoM", EnumMap.class);
json.setSerializer(EnumMap.class, new Json.Serializer() {
@Override
public void write(Json json, EnumMap object, Class knownType) {
json.writeObjectStart(EnumMap.class, knownType);
Iterator, Object>> es = new EnumMap.Entries