List of usage examples for com.google.gson GsonBuilder registerTypeAdapterFactory
public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory)
From source file:no.uis.service.ws.studinfosolr.impl.SolrUpdaterImpl.java
License:Apache License
@PostConstruct public void init() { GsonBuilder builder = new GsonBuilder(); if (typeAdapters != null) { for (Map.Entry<Class<?>, TypeAdapter<?>> adapter : typeAdapters.entrySet()) { builder.registerTypeAdapter(adapter.getKey(), adapter.getValue()); }/*from ww w. j a v a 2s.co m*/ // for <any/> element builder.registerTypeAdapterFactory( TypeAdapters.newTypeHierarchyFactory(ElementImpl.class, new XmlElementAdapter())); } gson = builder.create(); StringConverterUtil.registerConverter(new InngarIStudieprogramConverter(), InngarIStudieprogram.class); StringConverter collectionConverter = new CollectionConverter(); StringConverterUtil.registerConverter(collectionConverter, List.class); StringConverterUtil.registerConverter(collectionConverter, Set.class); StringConverterUtil.registerConverter(new ObligoppgaveConverter(), Obligoppgave.class); }
From source file:org.apache.zeppelin.jupyter.JupyterUtil.java
License:Apache License
private Gson getGson(GsonBuilder gsonBuilder) { return gsonBuilder.registerTypeAdapterFactory(cellTypeFactory).registerTypeAdapterFactory(outputTypeFactory) .create();//from w w w . j a v a 2 s . c om }
From source file:org.eclipse.recommenders.utils.gson.GsonUtil.java
License:Open Source License
public static synchronized Gson getInstance() { if (gson == null) { final GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(VmMethodName.class, new MethodNameTypeAdapter()); builder.registerTypeAdapter(IMethodName.class, new MethodNameTypeAdapter()); builder.registerTypeAdapter(VmTypeName.class, new TypeNameTypeAdapter()); builder.registerTypeAdapter(ITypeName.class, new TypeNameTypeAdapter()); builder.registerTypeAdapter(VmFieldName.class, new FieldNameTypeAdapter()); builder.registerTypeAdapter(IFieldName.class, new FieldNameTypeAdapter()); builder.registerTypeAdapter(VmPackageName.class, new PackageNameTypeAdapter()); builder.registerTypeAdapter(IPackageName.class, new PackageNameTypeAdapter()); builder.registerTypeAdapter(File.class, new FileTypeAdapter()); builder.registerTypeAdapter(UUID.class, new UuidTypeAdapter()); builder.registerTypeAdapter(Date.class, new ISO8601DateParser()); builder.registerTypeAdapter(Multimap.class, new MultimapTypeAdapter()); builder.registerTypeAdapterFactory(new MultisetTypeAdapterFactory()); builder.enableComplexMapKeySerialization(); builder.setPrettyPrinting();//from w w w. j a va 2 s . c o m gson = builder.create(); } return gson; }
From source file:org.egov.api.controller.core.ApiResponse.java
License:Open Source License
public ResponseEntity<String> _getResponse(Object data, String type, String message, HttpStatus httpStatus) { GsonBuilder gsonBuilder = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting(); if (this.dataAdapter != null) { gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY) .registerTypeAdapter(this.getDataAdapter().getBaseObject(), this.getDataAdapter()); }/* www . j a v a 2s . co m*/ JsonElement je = null; Gson gson = gsonBuilder.create(); status.addProperty("type", type); status.addProperty("message", message); JsonObject jo = new JsonObject(); jo.add("status", status); if (this.dataAdapter != null) { je = gson.toJsonTree(data, this.dataAdapter.getTypeToken()); } else { je = gson.toJsonTree(data); } jo.add("result", je); return new ResponseEntity<String>(gson.toJson(jo), httpStatus); }
From source file:org.egov.eis.web.controller.masters.employee.SearchEmployeeController.java
License:Open Source License
public String toJSON(final Object object) { final GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY); final Gson gson = gsonBuilder.registerTypeAdapter(Employee.class, new EmployeeAdaptor()).create(); final String json = gson.toJson(object); return json;/*from w ww . j a v a2 s . c om*/ }
From source file:org.helios.apmrouter.dataservice.json.marshalling.GSONJSONMarshaller.java
License:Open Source License
/** * Creates a new Gson instance based on the settings in this bean. *//*from w w w. java 2 s . c o m*/ protected void update() { GsonBuilder builder = new GsonBuilder(); if (prettyPrint) builder.setPrettyPrinting(); if (disableHtmlEscaping) builder.disableHtmlEscaping(); if (!adapterInstances.isEmpty()) { for (Map.Entry<Type, Object> entry : adapterInstances.entrySet()) { builder.registerTypeAdapter(entry.getKey(), entry.getValue()); } } if (!adapterFactoryInstances.isEmpty()) { for (TypeAdapterFactory taf : adapterFactoryInstances) { builder.registerTypeAdapterFactory(taf); } } gson = builder.create(); }
From source file:org.immutables.mongo.fixture.ManualStorage.java
License:Apache License
private static Gson createGson() { GsonBuilder gsonBuilder = new GsonBuilder(); for (TypeAdapterFactory factory : ServiceLoader.load(TypeAdapterFactory.class)) { gsonBuilder.registerTypeAdapterFactory(factory); }/* ww w . j av a 2s. c om*/ return gsonBuilder.create(); }
From source file:org.immutables.mongo.fixture.MongoContext.java
License:Apache License
private static com.google.gson.Gson createGson() { GsonBuilder gson = new GsonBuilder(); for (TypeAdapterFactory factory : ServiceLoader.load(TypeAdapterFactory.class)) { gson.registerTypeAdapterFactory(factory); }// w ww .j a v a 2 s . c o m // register custom serializer for polymorphic Holder final HolderJsonSerializer custom = new HolderJsonSerializer(); gson.registerTypeAdapter(Holder.class, custom); gson.registerTypeAdapter(ImmutableHolder.class, custom); return gson.create(); }
From source file:org.jclouds.json.config.GsonModule.java
License:Apache License
@SuppressWarnings("rawtypes") @Provides//w w w . j a va2 s. co m @Singleton Gson provideGson(TypeAdapter<JsonBall> jsonAdapter, DateAdapter adapter, ByteListAdapter byteListAdapter, ByteArrayAdapter byteArrayAdapter, PropertiesAdapter propertiesAdapter, JsonAdapterBindings bindings, CredentialsAdapterFactory credentialsAdapterFactory, OptionalTypeAdapterFactory optional, SetTypeAdapterFactory set, ImmutableSetTypeAdapterFactory immutableSet, MapTypeAdapterFactory map, MultimapTypeAdapterFactory multimap, IterableTypeAdapterFactory iterable, CollectionTypeAdapterFactory collection, ListTypeAdapterFactory list, ImmutableListTypeAdapterFactory immutableList, FluentIterableTypeAdapterFactory fluentIterable, ImmutableMapTypeAdapterFactory immutableMap, DefaultExclusionStrategy exclusionStrategy) { FieldNamingStrategy serializationPolicy = new AnnotationOrNameFieldNamingStrategy( ImmutableSet.of(new ExtractSerializedName(), new ExtractNamed())); GsonBuilder builder = new GsonBuilder().setFieldNamingStrategy(serializationPolicy) .setExclusionStrategies(exclusionStrategy); // simple (type adapters) builder.registerTypeAdapter(Properties.class, propertiesAdapter.nullSafe()); builder.registerTypeAdapter(Date.class, adapter.nullSafe()); builder.registerTypeAdapter(byte[].class, byteArrayAdapter.nullSafe()); builder.registerTypeAdapter(JsonBall.class, jsonAdapter.nullSafe()); builder.registerTypeAdapterFactory(credentialsAdapterFactory); builder.registerTypeAdapterFactory(optional); builder.registerTypeAdapterFactory(iterable); builder.registerTypeAdapterFactory(collection); builder.registerTypeAdapterFactory(list); builder.registerTypeAdapter(new TypeToken<List<Byte>>() { }.getType(), byteListAdapter.nullSafe()); builder.registerTypeAdapterFactory(immutableList); builder.registerTypeAdapterFactory(set); builder.registerTypeAdapterFactory(immutableSet); builder.registerTypeAdapterFactory(map); builder.registerTypeAdapterFactory(multimap); builder.registerTypeAdapterFactory(fluentIterable); builder.registerTypeAdapterFactory(immutableMap); AnnotationConstructorNamingStrategy deserializationPolicy = new AnnotationConstructorNamingStrategy( ImmutableSet.of(ConstructorProperties.class, SerializedNames.class, Inject.class), ImmutableSet.of(new ExtractNamed())); builder.registerTypeAdapterFactory(new DeserializationConstructorAndReflectiveTypeAdapterFactory( new ConstructorConstructor(ImmutableMap.<Type, InstanceCreator<?>>of()), serializationPolicy, Excluder.DEFAULT, deserializationPolicy)); // complicated (serializers/deserializers as they need context to operate) builder.registerTypeHierarchyAdapter(Enum.class, new EnumTypeAdapterThatReturnsFromValue()); for (Map.Entry<Type, Object> binding : bindings.getBindings().entrySet()) { builder.registerTypeAdapter(binding.getKey(), binding.getValue()); } for (TypeAdapterFactory factory : bindings.getFactories()) { builder.registerTypeAdapterFactory(factory); } return builder.create(); }
From source file:org.kairosdb.core.http.rest.json.GsonParser.java
License:Apache License
@Inject public GsonParser(AggregatorFactory aggregatorFactory, GroupByFactory groupByFactory) { m_aggregatorFactory = aggregatorFactory; m_groupByFactory = groupByFactory;/*from ww w .ja va 2 s . c o m*/ m_descriptorMap = new HashMap<Class, Map<String, PropertyDescriptor>>(); GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapterFactory(new LowercaseEnumTypeAdapterFactory()); builder.registerTypeAdapter(TimeUnit.class, new TimeUnitDeserializer()); builder.registerTypeAdapter(Metric.class, new MetricDeserializer()); m_gson = builder.create(); }