Comments (16)
I could use that, too. Basically what's missing is some kind of
serializeInternal()
method that ignores type adapters registered for this Type.
Original comment by [email protected]
on 20 Mar 2009 at 2:17
from gson.
Original comment by inder123
on 28 Mar 2009 at 5:58
- Added labels: Milestone-Release1.4
from gson.
I would absolutely love this feature as well. See this thread for related
discussion:
http://groups.google.com/group/google-gson/browse_thread/thread/a87d5d47b83d0cbe
Original comment by [email protected]
on 17 Jul 2009 at 5:13
from gson.
deferred to a future release
Original comment by inder123
on 29 Sep 2009 at 9:10
- Added labels: Milestone-Release1.5
- Removed labels: Milestone-Release1.4
from gson.
what about cloning the src object, serializing it using context and then do all
the
post-processing you want?
Didn't try this though, I'm just conjecturing....
Original comment by [email protected]
on 27 Apr 2010 at 4:04
from gson.
Was this actually fixed? I'm not sure it is in 1.5, even though it is marked
with the 1.5 milestone.
Relying on clone() is a really bad idea.
Original comment by [email protected]
on 26 Oct 2010 at 4:24
from gson.
I could use this feature too. I do not believed it was fixed in 1.5 or current
trunk version.
Original comment by [email protected]
on 31 Oct 2010 at 3:24
from gson.
Original comment by inder123
on 2 Nov 2010 at 11:59
- Removed labels: Milestone-Release1.5
from gson.
I experimented with this. We might be able to use the ancestors stack as a
hint. Whenever a serializer+object pair exists on the ancestors stack, we
should skip that serializer when recursively asked to serialize that object.
We'll either eventually serialize it with a lower-level serializer, or we'll
run out and we know we've hit a circular reference.
Original comment by limpbizkit
on 9 Nov 2010 at 8:03
from gson.
I would think that context.defaultWriteObject() (eg similar to the java object
serialization mechanism) would make a lot of sense.
Original comment by [email protected]
on 21 Jan 2011 at 4:58
from gson.
You can do this in Gson 2.1 with TypeAdapterFactory and Gson.getNextAdapter.
Original comment by limpbizkit
on 29 Dec 2011 at 5:37
- Changed state: WontFix
from gson.
Issue 44 has been merged into this issue.
Original comment by limpbizkit
on 29 Dec 2011 at 5:50
from gson.
Does a Gson.getNextAdapter mechanism solve the following issue?
class Obj
...
class A extends Obj
Obj subobject;
...
Now assume we have registered:
public class ObjSerializer implements JsonSerializer<Obj> {
public JsonElement serialize(Obj obj, Type t, JSC jsc) {
JsonObject json = new JsonObject();
json.addProperty("name", obj.getClass());
json.add("value", jsc.serialize(obj, t));
return json;
}
}
What "should" happen is that:
A a = new A();
...
gson.toJson(a);
should have a call sequence that looks like:
ObjSerializer.serialize(a, ...)
getNextAdapter().serialize(a, ...)
ObjSerializer.serialize(subobject, ...)
getNextAdapter().serialize(subobject, ...)
That is, can we both avoid infinite recursion while also respecting nested
registered objects' serialize mechanisms?
http://code.google.com/p/google-gson/issues/detail?id=43#c9 seems like it
solves this problem, but
http://code.google.com/p/google-gson/issues/detail?id=43#c11 doesn't seem to.
Original comment by [email protected]
on 6 Feb 2012 at 9:27
from gson.
Yeah, you want getNextAdapter. That API was hidden in Gson 2.1 because we
weren't sure that name was right, but its there in SVN. I'll write up an
example and post it here...
Original comment by [email protected]
on 7 Feb 2012 at 3:21
from gson.
Here's a big example that demonstrates all of the moving parts of
getNextAdapter. Drink maps to 'Obj' and MixedDrink maps to 'A' in your model.
Note that we're using the new streaming TypeAdapter API and not the tree-based
JsonSerializer/JsonDeserializer API. Only the new streaming API gives you
access to the next type adapter in the chain.
package com.google.gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
public class GetNextAdapterExample {
static class Drink {
protected final String name;
Drink(String name) {
this.name = name;
}
@Override public String toString() {
return name;
}
}
static class MixedDrink extends Drink {
private final Drink mix;
private final String alcohol;
MixedDrink(String name, Drink mix, String alcohol) {
super(name);
this.mix = mix;
this.alcohol = alcohol;
}
@Override public String toString() {
return name + " (" + mix + "+" + alcohol + ")";
}
}
public static void main(String[] args) {
Drink orangeJuice = new Drink("Orange Juice");
MixedDrink screwdriver = new MixedDrink("Screwdriver", orangeJuice, "Vodka");
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new DrinkTypeAdapterFactory())
.create();
// exercise toJson
System.out.println(gson.toJson(orangeJuice));
System.out.println(gson.toJson(screwdriver));
// exercise fromJson
String s = "{'name':'Orange Juice','virgin':true}";
String t = "{'mix':{'name':'Orange Juice','virgin':true},'alcohol':'Vodka','name':'Screwdriver'}";
System.out.println(gson.fromJson(s, Drink.class));
System.out.println(gson.fromJson(t, Drink.class));
}
static class DrinkTypeAdapterFactory implements TypeAdapterFactory {
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (!Drink.class.isAssignableFrom(type.getRawType())) {
return null; // this class only serializes 'Drink' and its subtypes
}
/*
* Lookup type adapters to do the actual work. We use getNextAdapter
* to avoid getting 'this' on the types that this factory supports.
*/
final TypeAdapter<Drink> drinkAdapter
= gson.getNextAdapter(this, TypeToken.get(Drink.class));
final TypeAdapter<MixedDrink> mixedDrinkAdapter
= gson.getNextAdapter(this, TypeToken.get(MixedDrink.class));
/*
* The JsonElement type adapter is always handy when we want to
* tweak what our delegate type adapter created.
*/
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
/**
* Now that we have some helpers, create the tweaked type adapter.
*/
TypeAdapter<Drink> result = new TypeAdapter<Drink>() {
@Override public void write(JsonWriter out, Drink value) throws IOException {
if (value instanceof MixedDrink) {
// write mixed drinks out normally
mixedDrinkAdapter.write(out, (MixedDrink) value);
} else {
/*
* Always add a 'virgin' attribute on non-mixed drinks.
* This takes three steps:
* 1. Get the delegate to serialize to a JsonObject,
* 2. Add our extra property to that JsonObject.
* 3. Serialize that to the stream.
*/
JsonObject object = drinkAdapter.toJsonTree(value).getAsJsonObject();
object.add("virgin", new JsonPrimitive(true));
elementAdapter.write(out, object);
}
}
@Override public Drink read(JsonReader in) throws IOException {
/*
* Use the appropriate type adapter based on the contents
* of the stream.
*/
JsonObject object = elementAdapter.read(in).getAsJsonObject();
if (object.has("alcohol")) {
return mixedDrinkAdapter.fromJsonTree(object);
} else {
return drinkAdapter.fromJsonTree(object);
}
}
}.nullSafe(); // so we don't have to check for null on the stream
return (TypeAdapter<T>) result;
}
}
}
Original comment by [email protected]
on 7 Feb 2012 at 4:55
from gson.
Two notes about the example from the previous comment:
1) You'll need at least GSon V2.2.
2) gson.getNextAdapter was renamed to gson.getDelegateAdapter
Original comment by [email protected]
on 26 Nov 2012 at 3:07
from gson.
Related Issues (20)
- Javascript HOT 22
- Gson cannot serialize subclass who override a field HOT 1
- Add support for Optional<T> in parsing instead of null HOT 4
- Poor performance for String serialization HOT 1
- IllegalStateException: TypeToken must be created when using GSON 2.10.1 and R8 on Android HOT 6
- Assertion is Missed to Validate testWriterCloseIsIdempotent Test for JsonWriter's Idempotency
- Assertion is Missed to Validate `testWriterCloseIsIdempotent` Test for JsonWriter's Idempotency HOT 2
- Split `testWriteHtmlSafe` into Two Separate Test Cases for Improved Test Granularity HOT 2
- GSON deserialization fails with ClassCastException: Cannot cast com.google.gson.internal.LinkedTreeMap to androidx.collection.ArrayMap HOT 1
- OSS-Fuzz issue 67620 HOT 3
- Edge case where R8 removes the TypeToken's generic type (while consuming the library's recently updated proguard rules)
- `JsonReader#beginObject` should throw checked exceptions, instead throws runtime `IllegalStateException`. HOT 2
- Serialization failure at index 0 and 1 in camel case field naming convention HOT 2
- [2.11.0] error_prone_annotations dependency HOT 5
- The module declaration in version 2.11.0 is not visible HOT 11
- TypeAdapterFactory INTEGER_FACTORY will change INTEGER type to LONG type HOT 2
- errorprone cannot be considered optional HOT 4
- Can't read multiline string in JSON5 HOT 3
- Performance regressions from 2.10.1 to 2.11.0 on R8 minification task HOT 2
- Implement fallback if class `java.security.AccessController` does not exist HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gson.