X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcom%2Fgoogle%2Fgson%2FTypeAdapter.java;fp=src%2Fcom%2Fgoogle%2Fgson%2FTypeAdapter.java;h=58dd64241e08e41d6cd4d71be47a9addbf4c0e0d;hb=cfd903b6b5113fa28991db19bf98d0340ae709a4;hp=0000000000000000000000000000000000000000;hpb=819197b2305a94ab4924c5ae37a3fad762078448;p=unical.git diff --git a/src/com/google/gson/TypeAdapter.java b/src/com/google/gson/TypeAdapter.java new file mode 100644 index 0000000..58dd642 --- /dev/null +++ b/src/com/google/gson/TypeAdapter.java @@ -0,0 +1,286 @@ +/* + * Copyright (C) 2011 Google Inc. + * + * 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.google.gson; + +import com.google.gson.internal.bind.JsonTreeWriter; +import com.google.gson.internal.bind.JsonTreeReader; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; +import com.google.gson.stream.JsonWriter; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; + +/** + * Converts Java objects to and from JSON. + * + *
{@code + * + * public class PointAdapter extends TypeAdapter+ * With this type adapter installed, Gson will convert {@code Points} to JSON as + * strings like {@code "5,8"} rather than objects like {@code {"x":5,"y":8}}. In + * this case the type adapter binds a rich Java class to a compact JSON value. + * + *{ + * public Point read(JsonReader reader) throws IOException { + * if (reader.peek() == JsonToken.NULL) { + * reader.nextNull(); + * return null; + * } + * String xy = reader.nextString(); + * String[] parts = xy.split(","); + * int x = Integer.parseInt(parts[0]); + * int y = Integer.parseInt(parts[1]); + * return new Point(x, y); + * } + * public void write(JsonWriter writer, Point value) throws IOException { + * if (value == null) { + * writer.nullValue(); + * return; + * } + * String xy = value.getX() + "," + value.getY(); + * writer.value(xy); + * } + * }}
The {@link #read(JsonReader) read()} method must read exactly one value
+ * and {@link #write(JsonWriter,Object) write()} must write exactly one value.
+ * For primitive types this is means readers should make exactly one call to
+ * {@code nextBoolean()}, {@code nextDouble()}, {@code nextInt()}, {@code
+ * nextLong()}, {@code nextString()} or {@code nextNull()}. Writers should make
+ * exactly one call to one of value()
or nullValue()
.
+ * For arrays, type adapters should start with a call to {@code beginArray()},
+ * convert all elements, and finish with a call to {@code endArray()}. For
+ * objects, they should start with {@code beginObject()}, convert the object,
+ * and finish with {@code endObject()}. Failing to convert a value or converting
+ * too many values may cause the application to crash.
+ *
+ *
Type adapters should be prepared to read null from the stream and write it + * to the stream. Alternatively, they should use {@link #nullSafe()} method while + * registering the type adapter with Gson. If your {@code Gson} instance + * has been configured to {@link GsonBuilder#serializeNulls()}, these nulls will be + * written to the final document. Otherwise the value (and the corresponding name + * when writing to a JSON object) will be omitted automatically. In either case + * your type adapter must handle null. + * + *
To use a custom type adapter with Gson, you must register it with a + * {@link GsonBuilder}:
{@code + * + * GsonBuilder builder = new GsonBuilder(); + * builder.registerTypeAdapter(Point.class, new PointAdapter()); + * // if PointAdapter didn't check for nulls in its read/write methods, you should instead use + * // builder.registerTypeAdapter(Point.class, new PointAdapter().nullSafe()); + * ... + * Gson gson = builder.create(); + * }+ * + * @since 2.1 + */ +// non-Javadoc: +// +//
A type adapter registered with Gson is automatically invoked while serializing +// or deserializing JSON. However, you can also use type adapters directly to serialize +// and deserialize JSON. Here is an example for deserialization:
{@code +// +// String json = "{'origin':'0,0','points':['1,2','3,4']}"; +// TypeAdapter+// And an example for serialization:graphAdapter = gson.getAdapter(Graph.class); +// Graph graph = graphAdapter.fromJson(json); +// }
{@code +// +// Graph graph = new Graph(...); +// TypeAdapter+// +//graphAdapter = gson.getAdapter(Graph.class); +// String json = graphAdapter.toJson(graph); +// }
Type adapters are type-specific. For example, a {@code
+// TypeAdapter
+ * {@code
+ *
+ * Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
+ * new TypeAdapter
+ * You can avoid this boilerplate handling of nulls by wrapping your type adapter with
+ * this method. Here is how we will rewrite the above example:
+ * {@code
+ *
+ * Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
+ * new TypeAdapter
+ * Note that we didn't need to check for nulls in our type adapter after we used nullSafe.
+ */
+ public final TypeAdapter