List of usage examples for com.google.gson JsonDeserializationContext deserialize
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException;
From source file:com.newtouch.watchman.echarts.utils.json.SeriesDeserializer.java
License:Open Source License
@Override /**//from w w w . j av a 2 s.c om * json,typeOfT,context * * @param json * @param typeOfT * @param context */ public Series deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final JsonObject jsonObject = json.getAsJsonObject(); String _type = jsonObject.get("type").getAsString(); SeriesType type = SeriesType.valueOf(null, _type); Series series = null; switch (type) { case line: series = context.deserialize(jsonObject, Line.class); break; case bar: series = context.deserialize(jsonObject, Bar.class); break; case scatter: series = context.deserialize(jsonObject, Scatter.class); break; case funnel: series = context.deserialize(jsonObject, Funnel.class); break; case pie: series = context.deserialize(jsonObject, Pie.class); break; case force: series = context.deserialize(jsonObject, Force.class); break; case gauge: series = context.deserialize(jsonObject, Gauge.class); break; case map: series = context.deserialize(jsonObject, Map.class); break; case island: series = context.deserialize(jsonObject, Island.class); break; case k: series = context.deserialize(jsonObject, K.class); break; case radar: series = context.deserialize(jsonObject, Radar.class); break; case chord: series = context.deserialize(jsonObject, Chord.class); break; } return series; }
From source file:com.optimizely.ab.config.parser.DatafileGsonDeserializer.java
License:Apache License
@Override public ProjectConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); String accountId = jsonObject.get("accountId").getAsString(); String projectId = jsonObject.get("projectId").getAsString(); String revision = jsonObject.get("revision").getAsString(); String version = jsonObject.get("version").getAsString(); int datafileVersion = Integer.parseInt(version); // generic list type tokens Type groupsType = new TypeToken<List<Group>>() { }.getType();/*from w w w . j a va 2 s. co m*/ Type experimentsType = new TypeToken<List<Experiment>>() { }.getType(); Type attributesType = new TypeToken<List<Attribute>>() { }.getType(); Type eventsType = new TypeToken<List<EventType>>() { }.getType(); Type audienceType = new TypeToken<List<Audience>>() { }.getType(); Type typedAudienceType = new TypeToken<List<TypedAudience>>() { }.getType(); List<Group> groups = context.deserialize(jsonObject.get("groups").getAsJsonArray(), groupsType); List<Experiment> experiments = context.deserialize(jsonObject.get("experiments").getAsJsonArray(), experimentsType); List<Attribute> attributes; attributes = context.deserialize(jsonObject.get("attributes"), attributesType); List<EventType> events = context.deserialize(jsonObject.get("events").getAsJsonArray(), eventsType); List<Audience> audiences = Collections.emptyList(); if (jsonObject.has("audiences")) { audiences = context.deserialize(jsonObject.get("audiences").getAsJsonArray(), audienceType); } List<Audience> typedAudiences = null; if (jsonObject.has("typedAudiences")) { typedAudiences = context.deserialize(jsonObject.get("typedAudiences").getAsJsonArray(), typedAudienceType); } boolean anonymizeIP = false; if (datafileVersion >= Integer.parseInt(DatafileProjectConfig.Version.V3.toString())) { anonymizeIP = jsonObject.get("anonymizeIP").getAsBoolean(); } List<FeatureFlag> featureFlags = null; List<Rollout> rollouts = null; Boolean botFiltering = null; if (datafileVersion >= Integer.parseInt(DatafileProjectConfig.Version.V4.toString())) { Type featureFlagsType = new TypeToken<List<FeatureFlag>>() { }.getType(); featureFlags = context.deserialize(jsonObject.getAsJsonArray("featureFlags"), featureFlagsType); Type rolloutsType = new TypeToken<List<Rollout>>() { }.getType(); rollouts = context.deserialize(jsonObject.get("rollouts").getAsJsonArray(), rolloutsType); if (jsonObject.has("botFiltering")) botFiltering = jsonObject.get("botFiltering").getAsBoolean(); } return new DatafileProjectConfig(accountId, anonymizeIP, botFiltering, projectId, revision, version, attributes, audiences, typedAudiences, events, experiments, featureFlags, groups, rollouts); }
From source file:com.optimizely.ab.config.parser.GsonHelpers.java
License:Apache License
private static List<Variation> parseVariations(JsonArray variationJson, JsonDeserializationContext context) { List<Variation> variations = new ArrayList<Variation>(variationJson.size()); for (Object obj : variationJson) { JsonObject variationObject = (JsonObject) obj; String id = variationObject.get("id").getAsString(); String key = variationObject.get("key").getAsString(); Boolean featureEnabled = false; if (variationObject.has("featureEnabled") && !variationObject.get("featureEnabled").isJsonNull()) { featureEnabled = variationObject.get("featureEnabled").getAsBoolean(); }//from w w w . j a v a 2 s. c o m List<FeatureVariableUsageInstance> variableUsageInstances = null; // this is an existence check rather than a version check since it's difficult to pass data // across deserializers. if (variationObject.has("variables")) { Type featureVariableUsageInstancesType = new TypeToken<List<FeatureVariableUsageInstance>>() { }.getType(); variableUsageInstances = context.deserialize(variationObject.getAsJsonArray("variables"), featureVariableUsageInstancesType); } variations.add(new Variation(id, key, featureEnabled, variableUsageInstances)); } return variations; }
From source file:com.optimizely.ab.config.parser.GsonHelpers.java
License:Apache License
static FeatureFlag parseFeatureFlag(JsonObject featureFlagJson, JsonDeserializationContext context) { String id = featureFlagJson.get("id").getAsString(); String key = featureFlagJson.get("key").getAsString(); String layerId = featureFlagJson.get("rolloutId").getAsString(); JsonArray experimentIdsJson = featureFlagJson.getAsJsonArray("experimentIds"); List<String> experimentIds = new ArrayList<String>(); for (JsonElement experimentIdObj : experimentIdsJson) { experimentIds.add(experimentIdObj.getAsString()); }/*from w ww . j a v a 2 s . com*/ List<FeatureVariable> FeatureVariables = new ArrayList<>(); try { Type FeatureVariableType = new TypeToken<List<FeatureVariable>>() { }.getType(); FeatureVariables = context.deserialize(featureFlagJson.getAsJsonArray("variables"), FeatureVariableType); } catch (JsonParseException exception) { logger.warn("Unable to parse variables for feature \"" + key + "\". JsonParseException: " + exception); } return new FeatureFlag(id, key, layerId, experimentIds, FeatureVariables); }
From source file:com.optimizely.ab.config.parser.ProjectConfigGsonDeserializer.java
License:Apache License
@Override public ProjectConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); String accountId = jsonObject.get("accountId").getAsString(); String projectId = jsonObject.get("projectId").getAsString(); String revision = jsonObject.get("revision").getAsString(); String version = jsonObject.get("version").getAsString(); // generic list type tokens Type groupsType = new TypeToken<List<Group>>() { }.getType();/*from ww w. j av a 2s . c om*/ Type experimentsType = new TypeToken<List<Experiment>>() { }.getType(); Type attributesType = new TypeToken<List<Attribute>>() { }.getType(); Type eventsType = new TypeToken<List<EventType>>() { }.getType(); Type audienceType = new TypeToken<List<Audience>>() { }.getType(); List<Group> groups = context.deserialize(jsonObject.get("groups").getAsJsonArray(), groupsType); List<Experiment> experiments = context.deserialize(jsonObject.get("experiments").getAsJsonArray(), experimentsType); List<Attribute> attributes = context.deserialize(jsonObject.get("dimensions").getAsJsonArray(), attributesType); List<EventType> events = context.deserialize(jsonObject.get("events").getAsJsonArray(), eventsType); List<Audience> audiences = context.deserialize(jsonObject.get("audiences").getAsJsonArray(), audienceType); return new ProjectConfig(accountId, projectId, version, revision, groups, experiments, attributes, events, audiences); }
From source file:com.orange.homenap.gson.ActionListAdapter.java
License:Open Source License
@Override public List<Action> deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException { JsonArray array = jsonElement.getAsJsonArray(); List<Action> actionList = new ArrayList<Action>(); for (int i = 0; i < array.size(); i++) { JsonElement element = array.get(i); JsonObject jsonObject = element.getAsJsonObject(); Action action = context.deserialize(jsonObject, Action.class); action.setProperties(new HashMap<String, Object>()); if (!jsonObject.get("properties").toString().equals("{}")) { //System.out.println(jsonObject.get("properties").toString()); JsonObject properties = jsonObject.get("properties").getAsJsonObject(); /*List<String> resources; JsonArray resourcesArray = properties.get("resources").getAsJsonArray(); //from ww w . jav a 2 s. com for(int j = 0; j < resourcesArray.size(); j++) resources.add(context.deserialize()) resources.add(resourcesArray.get(j).)); */ List<String> resources = context.deserialize(properties.get("resources").getAsJsonArray(), (new TypeToken<List<String>>() { }).getType()); action.getProperties().put("resources", resources); List<Component> components = context.deserialize(properties.get("components"), (new TypeToken<List<Component>>() { }).getType()); action.getProperties().put("components", components); List<Architecture> architectures = context.deserialize(properties.get("architectures"), (new TypeToken<List<Architecture>>() { }).getType()); action.getProperties().put("architectures", architectures); List<Device> devices = context.deserialize(properties.get("devices"), (new TypeToken<List<Device>>() { }).getType()); action.getProperties().put("devices", devices); } actionList.add(action); } return actionList; }
From source file:com.perl5.lang.perl.idea.run.debugger.PerlStackFrame.java
License:Apache License
@Nullable @Override/*from w w w . j a va 2 s . co m*/ public XDebuggerEvaluator getEvaluator() { return new XDebuggerEvaluator() { @Override public void evaluate(@NotNull String expression, @NotNull final XEvaluationCallback callback, @Nullable XSourcePosition expressionPosition) { PerlDebugThread thread = myPerlExecutionStack.getSuspendContext().getDebugThread(); thread.sendCommandAndGetResponse("e", new PerlEvalRequestDescriptor(expression), new PerlDebuggingTransactionHandler() { @Override public void run(JsonObject jsonObject, JsonDeserializationContext jsonDeserializationContext) { PerlEvalResponseDescriptor descriptor = jsonDeserializationContext.deserialize( jsonObject.getAsJsonObject("data"), PerlEvalResponseDescriptor.class); if (descriptor == null) { callback.errorOccurred( "Something bad happened on Perl side. Report to plugin devs."); } else if (descriptor.isError()) { callback.errorOccurred(descriptor.getResult().getValue()); } else { callback.evaluated( new PerlXNamedValue(descriptor.getResult(), PerlStackFrame.this)); } } }); } }; }
From source file:com.perl5.lang.perl.idea.run.debugger.protocol.PerlDebuggingEventsDeserializer.java
License:Apache License
@Override public PerlDebuggingEvent deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { String event = jsonElement.getAsJsonObject().getAsJsonPrimitive("event").getAsString(); PerlDebuggingEvent eventObject = null; if (StringUtil.isNotEmpty(event)) { if (StringUtil.equals(event, "STOP")) { PerlDebuggingEventStop newEvent = new PerlDebuggingEventStop(); newEvent.setFrames((PerlStackFrameDescriptor[]) jsonDeserializationContext.deserialize( jsonElement.getAsJsonObject().getAsJsonArray("data"), PerlStackFrameDescriptor[].class)); eventObject = newEvent;//w w w.j a v a 2 s. c o m } else if (StringUtil.equals(event, "BREAKPOINT_REACHED")) { eventObject = jsonDeserializationContext.deserialize( jsonElement.getAsJsonObject().getAsJsonObject("data"), PerlDebuggingEventBreakpointReached.class); } else if (StringUtil.equals(event, "BREAKPOINT_SET")) { eventObject = jsonDeserializationContext.deserialize( jsonElement.getAsJsonObject().getAsJsonObject("data"), PerlDebuggingEventBreakpointSet.class); } else if (StringUtil.equals(event, "BREAKPOINT_DENIED")) { eventObject = jsonDeserializationContext.deserialize( jsonElement.getAsJsonObject().getAsJsonObject("data"), PerlDebuggingEventBreakpointDenied.class); } else if (StringUtil.equals(event, "READY")) { eventObject = new PerlDebuggingEventReady(); ((PerlDebuggingEventReady) eventObject).version = jsonElement.getAsJsonObject() .getAsJsonPrimitive("version").getAsString(); } else if (StringUtil.equals(event, "LOADED_FILES_DELTA")) { eventObject = jsonDeserializationContext.deserialize( jsonElement.getAsJsonObject().getAsJsonObject("data"), PerlDebuggingEventLoadedFiles.class); } else if (StringUtil.equals(event, "RESPONSE")) { int transactionId = jsonElement.getAsJsonObject().getAsJsonPrimitive("transactionId").getAsInt(); PerlDebuggingTransactionHandler transactionHandler = myPerlDebugThread .getTransactionHandler(transactionId); if (transactionHandler == null) { throw new RuntimeException("Missing transaction handler for transaction " + transactionId); } transactionHandler.run(jsonElement.getAsJsonObject(), jsonDeserializationContext); eventObject = new PerlDebuggingEventDumb(); } else { System.err.println("Unhandled event in request: " + jsonElement.getAsString()); } } else { System.err.println("Empty event in request: " + jsonElement.getAsString()); } return eventObject; }
From source file:com.perl5.lang.perl.util.PerlDebugUtil.java
License:Apache License
public static void requestAndComputeChildren(@NotNull final XCompositeNode node, final PerlStackFrame perlStackFrame, final int[] offset, final int size, String key) { PerlDebugThread thread = perlStackFrame.getPerlExecutionStack().getSuspendContext().getDebugThread(); final int frameSize = XCompositeNode.MAX_CHILDREN_TO_SHOW; thread.sendCommandAndGetResponse("getchildren", new PerlValueRequestDescriptor(offset[0], frameSize, key), new PerlDebuggingTransactionHandler() { @Override// w w w . j a v a 2s. com public void run(JsonObject jsonObject, JsonDeserializationContext jsonDeserializationContext) { PerlValueDescriptor[] descriptors = jsonDeserializationContext .deserialize(jsonObject.getAsJsonArray("data"), PerlValueDescriptor[].class); XValueChildrenList list = new XValueChildrenList(); for (PerlValueDescriptor descriptor : descriptors) { list.add(new PerlXNamedValue(descriptor, perlStackFrame)); offset[0]++; } boolean isLast = offset[0] >= size; node.addChildren(list, isLast); if (!isLast) { node.tooManyChildren(size - offset[0]); } } }); }
From source file:com.qualcomm.robotcore.hardware.LynxModuleMetaList.java
License:Open Source License
public static LynxModuleMetaList fromSerializationString(String serialization) { JsonDeserializer deserializer = new JsonDeserializer() { @Override/*from w ww . ja v a 2 s .c om*/ public Object deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { return context.deserialize(json, LynxModuleMeta.class); } }; Gson gson = new GsonBuilder().registerTypeAdapter(RobotCoreLynxModule.class, deserializer).create(); return gson.fromJson(serialization, LynxModuleMetaList.class); }