GithubHelp home page GithubHelp logo

Comments (4)

GoogleCodeExporter avatar GoogleCodeExporter commented on May 28, 2024
Install your own type adapter! And you should avoid reporting bugs against 
ancient versions of GSON.

Original comment by limpbizkit on 10 Oct 2011 at 2:07

  • Changed state: WontFix
  • Added labels: ****
  • Removed labels: ****

from google-gson.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 28, 2024
nevertheless this feature is often needed. I found the following code for a 
type adapter wheen googling
public class XMLGregorianCalendarConverter {
    public static class Serializer implements JsonSerializer {
        public Serializer() {
            super();
        }
        public JsonElement serialize(Object t, Type type,
                JsonSerializationContext jsonSerializationContext) {
            XMLGregorianCalendar xgcal = (XMLGregorianCalendar) t;
            return new JsonPrimitive(xgcal.toXMLFormat());
        }
    }
    public static class Deserializer implements JsonDeserializer {
        public Object deserialize(JsonElement jsonElement, Type type,
                JsonDeserializationContext jsonDeserializationContext) {
            try {
                return DatatypeFactory.newInstance().newXMLGregorianCalendar(
                        jsonElement.getAsString());
            } catch (Exception e) {
                return null;
            }
        }
    }
}
register this with
            GsonBuilder gson_builder = new GsonBuilder();
            gson_builder.registerTypeAdapter(XMLGregorianCalendar.class,
                    new XMLGregorianCalendarConverter.Serializer());
            gson_builder.registerTypeAdapter(XMLGregorianCalendar.class,
                    new XMLGregorianCalendarConverter.Deserializer());
            gson = gson_builder.create();


Original comment by [email protected] on 22 Mar 2013 at 4:15

  • Added labels: ****
  • Removed labels: ****

from google-gson.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 28, 2024
[deleted comment]

from google-gson.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 28, 2024
I had an issue with the above example. The "jsonElement.getAsString()" method 
would throw an UnSupportedOperation exception and the adapter would always 
returned null. I made a slight modification that worked for me. Here is what I 
used to be able to DeSerialize the XMLGregorianCalendar class to json:

public class XMLGregorianCalendarConverter {
    public static class Serializer implements JsonSerializer {

        public Serializer() {
            super();
        }
        @Override
        public JsonElement serialize(Object t, Type type,
                JsonSerializationContext jsonSerializationContext) {
            XMLGregorianCalendar xgcal = (XMLGregorianCalendar) t;
            return new JsonPrimitive(xgcal.toXMLFormat());
        }

    }
    public static class Deserializer implements JsonDeserializer {

        @Override
        public Object deserialize(JsonElement jsonElement, Type type,
                JsonDeserializationContext jsonDeserializationContext) {
            try {
                JsonObject obj  = jsonElement.getAsJsonObject();
                XMLGregorianCalendar xmlGregCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(obj.get("year").getAsInt(), 
                                                                     obj.get("month").getAsInt(), 
                                                                     obj.get("day").getAsInt(), 
                                                                     obj.get("hour").getAsInt(), 
                                                                     obj.get("minute").getAsInt(),obj.get("second").getAsInt(),
                                                                     0, obj.get("timezone").getAsInt());
                return xmlGregCalendar;
            } catch (Exception e) {
                return null;
            }
        }

    }
}

Original comment by [email protected] on 18 Oct 2013 at 2:59

  • Added labels: ****
  • Removed labels: ****

from google-gson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.