guochaiqi / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Escaper not public class -- can't extend JsonElement #239

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create class to extend JsonElement
2. implement toString(Appender,Escaper)
3. Cannot include Escaper, as it is not a public class

What is the expected output? What do you see instead?
I would expect to be able to include Escaper and implement the toString(...) 
abstract method, but since Escaper is package scope, I cannot.

What version of the product are you using? On what operating system?
OSX 10.6.4

Please provide any additional information below.

I would like to be able to extend JsonElement to provide a new json element 
type. The use case I'm working with is that I have a JSON block generated 
elsewhere in our application that I would like to just 'use' in json output 
created near the UI layer of the application. To avoid deserializing & 
reserializing the JSON block I already have, I would like to create a new 
JsonElement that can just "hand back" this pre-generated block of JSON. I was 
going to extend JsonElement to create such a class, but cannot, since 
toString(...) requires Escaper, and that is a package level class, instead of 
public.

Original issue reported on code.google.com by jbros...@gmail.com on 21 Sep 2010 at 3:58

GoogleCodeExporter commented 9 years ago
Can you address this with the streaming API?
http://sites.google.com/site/gson/streaming

Original comment by limpbizkit on 21 Mar 2011 at 9:20

GoogleCodeExporter commented 9 years ago
Following is my similar case which I need to do by implementing JsonElement and 
couldn't figure out how to do with streaming API. Any idea please?

Gson gson = new GsonBuilder()
  .registerTypeAdapter(MyRawJson.class, new MyRawJsonSerializer())
  .create();

interface MyRawJson {
  String getJson();
}

class MyRawJsonSerializer implements JsonSerializer<RawJson> {
  @Override
  public JsonElement serialize(final MyRawJson src, Type type, JsonSerializationContext ctx) {
    return new JsonElement() {
      @Override
      protected void toString(Appendable sb, Escaper escaper) throws IOException {
        sb.append(src.getJson());
      }
    };
  }
}

Original comment by ali.sak...@gmail.com on 15 Jul 2011 at 10:30

GoogleCodeExporter commented 9 years ago
You're close. Use JsonParser to convert your JSON string to a JSON element, and 
then return that in your serialize() method.

Original comment by limpbizkit on 17 Jul 2011 at 4:44

GoogleCodeExporter commented 9 years ago
How to do this in the current gson version? I see in 2.2.2 the escaper class is 
gone and instead you use the JsonWriter.. 

I'm trying to extend JsonElement  to have an implementation that avoids 
escaping a string which is already escaped (performance wise, not going through 
each and every character).

Original comment by anmolsha...@gmail.com on 18 Mar 2013 at 5:44