Added support for AtomicLongArray.
Also added tests to ensure LongSerializationPolicy is honored.
This commit is contained in:
parent
caef762530
commit
11b26b5256
|
@ -30,10 +30,8 @@ import java.util.Collections;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicIntegerArray;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicLongArray;
|
||||
|
||||
import com.google.gson.internal.ConstructorConstructor;
|
||||
import com.google.gson.internal.Excluder;
|
||||
|
@ -224,6 +222,7 @@ public final class Gson {
|
|||
factories.add(TypeAdapters.ATOMIC_INTEGER_FACTORY);
|
||||
factories.add(TypeAdapters.ATOMIC_BOOLEAN_FACTORY);
|
||||
factories.add(TypeAdapters.newFactory(AtomicLong.class, atomicLongAdapter(longAdapter)));
|
||||
factories.add(TypeAdapters.newFactory(AtomicLongArray.class, atomicLongArrayAdapter(longAdapter)));
|
||||
factories.add(TypeAdapters.ATOMIC_INTEGER_ARRAY_FACTORY);
|
||||
factories.add(TypeAdapters.CHARACTER_FACTORY);
|
||||
factories.add(TypeAdapters.STRING_BUILDER_FACTORY);
|
||||
|
@ -344,6 +343,34 @@ public final class Gson {
|
|||
}
|
||||
}.nullSafe();
|
||||
}
|
||||
|
||||
private static TypeAdapter<AtomicLongArray> atomicLongArrayAdapter(final TypeAdapter<Number> longAdapter) {
|
||||
return new TypeAdapter<AtomicLongArray>() {
|
||||
@Override public void write(JsonWriter out, AtomicLongArray value) throws IOException {
|
||||
out.beginArray();
|
||||
for (int i = 0, length = value.length(); i < length; i++) {
|
||||
longAdapter.write(out, value.get(i));
|
||||
}
|
||||
out.endArray();
|
||||
}
|
||||
@Override public AtomicLongArray read(JsonReader in) throws IOException {
|
||||
List<Long> list = new ArrayList<Long>();
|
||||
in.beginArray();
|
||||
while (in.hasNext()) {
|
||||
long value = longAdapter.read(in).longValue();
|
||||
list.add(value);
|
||||
}
|
||||
in.endArray();
|
||||
int length = list.size();
|
||||
AtomicLongArray array = new AtomicLongArray(length);
|
||||
for (int i = 0; i < length; ++i) {
|
||||
array.set(i, list.get(i));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
}.nullSafe();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type adapter for {@code} type.
|
||||
*
|
||||
|
|
|
@ -20,8 +20,11 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicIntegerArray;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicLongArray;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.LongSerializationPolicy;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
|
@ -58,6 +61,16 @@ public class JavaUtilConcurrentLocksTest extends TestCase {
|
|||
assertEquals("10", json);
|
||||
}
|
||||
|
||||
public void testAtomicLongWithStringSerializationPolicy() throws Exception {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setLongSerializationPolicy(LongSerializationPolicy.STRING)
|
||||
.create();
|
||||
AtomicLongHolder target = gson.fromJson("{'value':'10'}", AtomicLongHolder.class);
|
||||
assertEquals(10, target.value.get());
|
||||
String json = gson.toJson(target);
|
||||
assertEquals("{\"value\":\"10\"}", json);
|
||||
}
|
||||
|
||||
public void testAtomicIntegerArray() throws Exception {
|
||||
AtomicIntegerArray target = gson.fromJson("[10, 13, 14]", AtomicIntegerArray.class);
|
||||
assertEquals(3, target.length());
|
||||
|
@ -67,4 +80,31 @@ public class JavaUtilConcurrentLocksTest extends TestCase {
|
|||
String json = gson.toJson(target);
|
||||
assertEquals("[10,13,14]", json);
|
||||
}
|
||||
|
||||
public void testAtomicLongArray() throws Exception {
|
||||
AtomicLongArray target = gson.fromJson("[10, 13, 14]", AtomicLongArray.class);
|
||||
assertEquals(3, target.length());
|
||||
assertEquals(10, target.get(0));
|
||||
assertEquals(13, target.get(1));
|
||||
assertEquals(14, target.get(2));
|
||||
String json = gson.toJson(target);
|
||||
assertEquals("[10,13,14]", json);
|
||||
}
|
||||
|
||||
public void testAtomicLongArrayWithStringSerializationPolicy() throws Exception {
|
||||
Gson gson = new GsonBuilder()
|
||||
.setLongSerializationPolicy(LongSerializationPolicy.STRING)
|
||||
.create();
|
||||
AtomicLongArray target = gson.fromJson("['10', '13', '14']", AtomicLongArray.class);
|
||||
assertEquals(3, target.length());
|
||||
assertEquals(10, target.get(0));
|
||||
assertEquals(13, target.get(1));
|
||||
assertEquals(14, target.get(2));
|
||||
String json = gson.toJson(target);
|
||||
assertEquals("[\"10\",\"13\",\"14\"]", json);
|
||||
}
|
||||
|
||||
private static class AtomicLongHolder {
|
||||
AtomicLong value;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user