2011-07-11 18:46:52 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-11-20 16:23:08 +01:00
|
|
|
package com.google.gson;
|
2011-07-11 18:46:52 +02:00
|
|
|
|
2011-11-20 16:23:08 +01:00
|
|
|
import com.google.gson.internal.bind.JsonElementReader;
|
|
|
|
import com.google.gson.internal.bind.JsonElementWriter;
|
2011-08-03 04:40:18 +02:00
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import com.google.gson.stream.JsonReader;
|
|
|
|
import com.google.gson.stream.JsonWriter;
|
2011-08-19 04:35:29 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.Reader;
|
|
|
|
import java.io.StringReader;
|
|
|
|
import java.io.StringWriter;
|
|
|
|
import java.io.Writer;
|
|
|
|
|
2011-11-20 16:23:08 +01:00
|
|
|
// TODO: nice documentation
|
|
|
|
|
2011-07-11 18:46:52 +02:00
|
|
|
public abstract class TypeAdapter<T> {
|
|
|
|
public abstract T read(JsonReader reader) throws IOException;
|
|
|
|
public abstract void write(JsonWriter writer, T value) throws IOException;
|
|
|
|
|
|
|
|
public final String toJson(T value) throws IOException {
|
|
|
|
StringWriter stringWriter = new StringWriter();
|
2011-12-02 23:57:30 +01:00
|
|
|
toJson(stringWriter, value);
|
2011-07-11 18:46:52 +02:00
|
|
|
return stringWriter.toString();
|
|
|
|
}
|
|
|
|
|
2011-12-02 23:57:30 +01:00
|
|
|
public final void toJson(Writer out, T value) throws IOException {
|
2011-07-11 18:46:52 +02:00
|
|
|
JsonWriter writer = new JsonWriter(out);
|
|
|
|
write(writer, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public final T fromJson(String json) throws IOException {
|
2011-12-02 23:57:30 +01:00
|
|
|
return fromJson(new StringReader(json));
|
2011-07-11 18:46:52 +02:00
|
|
|
}
|
|
|
|
|
2011-12-02 23:57:30 +01:00
|
|
|
public final T fromJson(Reader in) throws IOException {
|
2011-07-11 18:46:52 +02:00
|
|
|
JsonReader reader = new JsonReader(in);
|
2011-07-11 23:45:09 +02:00
|
|
|
reader.setLenient(true);
|
2011-07-11 18:46:52 +02:00
|
|
|
return read(reader);
|
|
|
|
}
|
|
|
|
|
2011-12-02 23:57:30 +01:00
|
|
|
public JsonElement toJsonTree(T src) {
|
2011-08-03 04:40:18 +02:00
|
|
|
try {
|
2011-09-30 08:32:33 +02:00
|
|
|
JsonElementWriter jsonWriter = new JsonElementWriter();
|
2011-08-03 04:40:18 +02:00
|
|
|
jsonWriter.setLenient(true);
|
|
|
|
write(jsonWriter, src);
|
2011-09-30 08:32:33 +02:00
|
|
|
return jsonWriter.get();
|
2011-08-03 04:40:18 +02:00
|
|
|
} catch (IOException e) {
|
2011-08-19 04:35:29 +02:00
|
|
|
throw new JsonIOException(e);
|
2011-08-03 04:40:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 23:57:30 +01:00
|
|
|
public T fromJsonTree(JsonElement json) {
|
2011-08-03 04:40:18 +02:00
|
|
|
try {
|
2011-09-30 05:47:30 +02:00
|
|
|
JsonReader jsonReader = new JsonElementReader(json);
|
2011-08-03 04:40:18 +02:00
|
|
|
jsonReader.setLenient(true);
|
|
|
|
return read(jsonReader);
|
|
|
|
} catch (IOException e) {
|
2011-08-19 04:35:29 +02:00
|
|
|
throw new JsonIOException(e);
|
2011-08-03 04:40:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-03 02:17:17 +02:00
|
|
|
public interface Factory {
|
2011-11-25 06:40:17 +01:00
|
|
|
<T> TypeAdapter<T> create(Gson gson, TypeToken<T> type);
|
2011-07-11 18:46:52 +02:00
|
|
|
}
|
|
|
|
}
|