Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom serializer - deserializer using GSON for a list of BasicNameValuePairs

I'm trying to implement a custom gson serializer/deserialiser for some list of BasicNameValuePair objects.

I saw the partial solution code (for serialization) here: How do I get Gson to serialize a list of basic name value pairs?

However I wanted to implement also deserialization and I tried my chances and the code is here:

package dto;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;


import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;


    public class KeyValuePairSerializer extends TypeAdapter<List<BasicNameValuePair>> {
        @Override
        public void write(JsonWriter out, List<BasicNameValuePair> data) throws IOException {
            out.beginObject();
            for(int i=0; i<data.size();i++){
                out.name(data.get(i).getName());
                out.value(data.get(i).getValue());
            }
            out.endObject();
        }

        @Override
        public List<BasicNameValuePair> read(JsonReader in) throws IOException {
            ArrayList<BasicNameValuePair> list=new ArrayList<BasicNameValuePair>();
             in.beginObject();
             while (in.hasNext()) {
               String key = in.nextName();
               String value = in.nextString();
               list.add(new BasicNameValuePair(key,value));
             }  
             in.endObject();   
             return list;
        }

}

Code to initialize and fill the list

ArrayList<BasicNameValuePair> postParameters=new ArrayList<BasicNameValuePair>();
postParameters.add(new BasicNameValuePair("some_key","some_value"));

And here is the code to use the new KeyValuePairSerializer class:

        GsonBuilder gsonBuilder= new GsonBuilder();
        gsonBuilder.registerTypeAdapter(KeyValuePairSerializer.class, new KeyValuePairSerializer());
        Gson gson1=gsonBuilder.create();

        //serialization works just fine in the next line
        String jsonUpdate=gson1.toJson(postParameters, KeyValuePairSerializer.class);


        ArrayList<BasicNameValuePair> postParameters2 = new ArrayList<BasicNameValuePair>();
        //postParameters2 = gson1.fromJson(jsonUpdate, KeyValuePairSerializer.class); //? how to cast properly
        //deserialization throws an error, it can't cast from ArrayList<BasicNameValuePair> to KeyValuePairSerializer
        gson1.fromJson(jsonUpdate, KeyValuePairSerializer.class);

The problem is that it throws an exception at the end and I don't know where exactly is the problem and still not sure how to write the last line to get the result back in the new postParameters2 ArrayList.

like image 783
Arise Avatar asked Mar 20 '13 11:03

Arise


1 Answers

Adapted from the GSON Collections Examples:

GsonBuilder gsonBuilder= new GsonBuilder();
gsonBuilder.registerTypeAdapter(KeyValuePairSerializer.class, new KeyValuePairSerializer());
Gson gson1=gsonBuilder.create();

Type collectionType = new TypeToken<ArrayList<BasicNameValuePair>>(){}.getType();
ArrayList<BasicNameValuePair> postParameters2 = gson1.fromJson(jsonUpdate, collectionType);
like image 190
Nachi Avatar answered Nov 12 '22 07:11

Nachi