List of usage examples for com.google.gson InstanceCreator InstanceCreator
InstanceCreator
From source file:org.rapla.server.jsonrpc.JsonServlet.java
License:Apache License
/** Create a default GsonBuilder with some extra types defined. */ public static GsonBuilder defaultGsonBuilder() { final GsonBuilder gb = new GsonBuilder(); gb.registerTypeAdapter(java.util.Set.class, new InstanceCreator<java.util.Set<Object>>() { @Override// ww w.j a v a 2 s . com public Set<Object> createInstance(final Type arg0) { return new HashSet<Object>(); } }); gb.registerTypeAdapter(java.util.Map.class, new MapDeserializer()); gb.registerTypeAdapter(java.sql.Date.class, new SqlDateDeserializer()); gb.registerTypeAdapter(java.sql.Timestamp.class, new SqlTimestampDeserializer()); return gb; }
From source file:org.raspinloop.config.GsonProperties.java
License:Apache License
public GsonProperties(HardwareEnumerator enumerator) { GsonBuilder builder = new GsonBuilder(); Collection<HardwareProperties> boards = enumerator.buildListImplementing(BoardHardware.class); boolean delegateExisting = false; for (HardwareProperties hardwareProps : boards) { if (hardwareProps instanceof BoardHardwareDelegate) { delegateExisting = true;//from w w w .ja v a 2 s . co m } } if (!delegateExisting) boards.add(new BoardHardwareDelegate()); builder = registerImpl(boards, builder, BoardHardware.class); builder = registerImpl(enumerator.buildListImplementing(BoardExtentionHardware.class), builder, BoardExtentionHardware.class); builder = registerImpl(enumerator.buildListImplementing(UARTComponent.class), builder, UARTComponent.class); builder = registerImpl(enumerator.buildListImplementing(I2CComponent.class), builder, I2CComponent.class); builder = registerImpl(enumerator.buildListImplementing(SPIComponent.class), builder, SPIComponent.class); builder.registerTypeAdapter(Pin.class, new InstanceCreator<PinImpl>() { public PinImpl createInstance(Type type) { return new PinImpl(); } }); builder.setExclusionStrategies(new PreventLoop()); gsonExt = builder.setPrettyPrinting().create(); }
From source file:org.uncertml.io.JSONParser.java
License:Apache License
/** * Register the individual instance creators with the GSON builder. * /*from ww w. j a v a 2 s. c o m*/ * @param builder <code>GsonBuilder</code> used to build the UncertML objects * from JSON representations. */ private void registerInstanceCreators(GsonBuilder builder) { builder.registerTypeAdapter(BetaDistribution.class, new InstanceCreator<BetaDistribution>() { @Override public BetaDistribution createInstance(Type arg0) { return new BetaDistribution(Arrays.asList(new Double[] { 0.5 }), Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(BinomialDistribution.class, new InstanceCreator<BinomialDistribution>() { @Override public BinomialDistribution createInstance(Type arg0) { return new BinomialDistribution(Arrays.asList(new Integer[] { 1 }), Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(CategoricalUniformDistribution.class, new InstanceCreator<CategoricalUniformDistribution>() { @Override public CategoricalUniformDistribution createInstance(Type arg0) { return new CategoricalUniformDistribution(Arrays.asList(new Integer[] { 1 })); } }); builder.registerTypeAdapter(CauchyDistribution.class, new InstanceCreator<CauchyDistribution>() { @Override public CauchyDistribution createInstance(Type arg0) { return new CauchyDistribution(Arrays.asList(new Double[] { 0.5 }), Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(ChiSquareDistribution.class, new InstanceCreator<ChiSquareDistribution>() { @Override public ChiSquareDistribution createInstance(Type arg0) { return new ChiSquareDistribution(Arrays.asList(new Integer[] { 1 })); } }); builder.registerTypeAdapter(DirichletDistribution.class, new InstanceCreator<DirichletDistribution>() { @Override public DirichletDistribution createInstance(Type arg0) { return new DirichletDistribution(Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(DiscreteUniformDistribution.class, new InstanceCreator<DiscreteUniformDistribution>() { @Override public DiscreteUniformDistribution createInstance(Type arg0) { return new DiscreteUniformDistribution(Arrays.asList(new Integer[] { 1 })); } }); builder.registerTypeAdapter(ExponentialDistribution.class, new InstanceCreator<ExponentialDistribution>() { @Override public ExponentialDistribution createInstance(Type arg0) { return new ExponentialDistribution(Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(FDistribution.class, new InstanceCreator<FDistribution>() { @Override public FDistribution createInstance(Type arg0) { return new FDistribution(Arrays.asList(new Integer[] { 1 }), Arrays.asList(new Integer[] { 1 })); } }); builder.registerTypeAdapter(GammaDistribution.class, new InstanceCreator<GammaDistribution>() { @Override public GammaDistribution createInstance(Type arg0) { return new GammaDistribution(Arrays.asList(new Double[] { 0.5 }), Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(NormalDistribution.class, new InstanceCreator<NormalDistribution>() { @Override public NormalDistribution createInstance(Type arg0) { return new NormalDistribution(Arrays.asList(new Double[] { 0.5 }), Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(GeometricDistribution.class, new InstanceCreator<GeometricDistribution>() { @Override public GeometricDistribution createInstance(Type arg0) { return new GeometricDistribution(Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(HypergeometricDistribution.class, new InstanceCreator<HypergeometricDistribution>() { @Override public HypergeometricDistribution createInstance(Type arg0) { return new HypergeometricDistribution(Arrays.asList(new Integer[] { 1 }), Arrays.asList(new Integer[] { 1 }), Arrays.asList(new Integer[] { 1 })); } }); builder.registerTypeAdapter(InverseGammaDistribution.class, new InstanceCreator<InverseGammaDistribution>() { @Override public InverseGammaDistribution createInstance(Type arg0) { return new InverseGammaDistribution(Arrays.asList(new Double[] { 0.5 }), Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(LaplaceDistribution.class, new InstanceCreator<LaplaceDistribution>() { @Override public LaplaceDistribution createInstance(Type arg0) { return new LaplaceDistribution(Arrays.asList(new Double[] { 0.5 }), Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(LogisticDistribution.class, new InstanceCreator<LogisticDistribution>() { @Override public LogisticDistribution createInstance(Type arg0) { return new LogisticDistribution(Arrays.asList(new Double[] { 0.5 }), Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(LogNormalDistribution.class, new InstanceCreator<LogNormalDistribution>() { @Override public LogNormalDistribution createInstance(Type arg0) { return new LogNormalDistribution(Arrays.asList(new Double[] { 0.5 }), Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(MultivariateNormalDistribution.class, new InstanceCreator<MultivariateNormalDistribution>() { @Override public MultivariateNormalDistribution createInstance(Type arg0) { CovarianceMatrix cm = new CovarianceMatrix(2, new double[] { 3.14, 0, 0, 3.14 }); MultivariateNormalDistribution multi = new MultivariateNormalDistribution( new double[] { 3.14, 6.28 }, cm); return multi; //return (MultivariateNormalDistribution) new Randomizer().generate(MultivariateNormalDistribution.class, 1); } }); builder.registerTypeAdapter(MultivariateStudentTDistribution.class, new InstanceCreator<MultivariateStudentTDistribution>() { @Override public MultivariateStudentTDistribution createInstance(Type arg0) { CovarianceMatrix cm = new CovarianceMatrix(2, new double[] { 3.14, 0, 0, 3.14 }); MultivariateStudentTDistribution multi = new MultivariateStudentTDistribution( new double[] { 3.14, 6.28 }, cm, new int[] { 1, 2 }); return multi; //return (MultivariateStudentTDistribution) new Randomizer().generate(MultivariateStudentTDistribution.class, 1); } }); builder.registerTypeAdapter(NegativeBinomialDistribution.class, new InstanceCreator<NegativeBinomialDistribution>() { @Override public NegativeBinomialDistribution createInstance(Type arg0) { return new NegativeBinomialDistribution(Arrays.asList(new Integer[] { 1 }), Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(MultinomialDistribution.class, new InstanceCreator<MultinomialDistribution>() { @Override public MultinomialDistribution createInstance(Type arg0) { return new MultinomialDistribution(2, Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(NormalInverseGammaDistribution.class, new InstanceCreator<NormalInverseGammaDistribution>() { @Override public NormalInverseGammaDistribution createInstance(Type arg0) { return new NormalInverseGammaDistribution(Arrays.asList(new Double[] { 0.5 }), Arrays.asList(new Double[] { 0.5 }), Arrays.asList(new Double[] { 0.5 }), Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(PoissonDistribution.class, new InstanceCreator<PoissonDistribution>() { @Override public PoissonDistribution createInstance(Type arg0) { return new PoissonDistribution(Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(StudentTDistribution.class, new InstanceCreator<StudentTDistribution>() { @Override public StudentTDistribution createInstance(Type arg0) { return new StudentTDistribution(Arrays.asList(new Double[] { 0.5 }), Arrays.asList(new Double[] { 0.5 }), Arrays.asList(new Integer[] { 1 })); } }); builder.registerTypeAdapter(UniformDistribution.class, new InstanceCreator<UniformDistribution>() { @Override public UniformDistribution createInstance(Type arg0) { return new UniformDistribution(Arrays.asList(new Double[] { 0.5 }), Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(WeibullDistribution.class, new InstanceCreator<WeibullDistribution>() { @Override public WeibullDistribution createInstance(Type arg0) { return new WeibullDistribution(Arrays.asList(new Double[] { 0.5 }), Arrays.asList(new Double[] { 0.5 })); } }); builder.registerTypeAdapter(ContinuousRealisation.class, new InstanceCreator<ContinuousRealisation>() { @Override public ContinuousRealisation createInstance(Type arg0) { return new ContinuousRealisation(Arrays.asList(new Double[] { 0.5 }), 0.2, "Test"); } }); builder.registerTypeAdapter(RandomSample.class, new InstanceCreator<RandomSample>() { @Override public RandomSample createInstance(Type arg0) { return new RandomSample( new ContinuousRealisation[] { new ContinuousRealisation(new double[] { 0.2 }) }, "Hello"); } }); builder.registerTypeAdapter(SystematicSample.class, new InstanceCreator<SystematicSample>() { @Override public SystematicSample createInstance(Type arg0) { return new SystematicSample( new ContinuousRealisation[] { new ContinuousRealisation(new double[] { 0.2 }) }, "Hello"); } }); builder.registerTypeAdapter(UnknownSample.class, new InstanceCreator<UnknownSample>() { @Override public UnknownSample createInstance(Type arg0) { return new UnknownSample( new ContinuousRealisation[] { new ContinuousRealisation(new double[] { 0.2 }) }, "Hello"); } }); builder.registerTypeAdapter(CategoricalMode.class, new InstanceCreator<CategoricalMode>() { @Override public CategoricalMode createInstance(Type arg0) { return new CategoricalMode(Arrays.asList(new String[] { "A" })); } }); builder.registerTypeAdapter(CentredMoment.class, new InstanceCreator<CentredMoment>() { @Override public CentredMoment createInstance(Type arg0) { return new CentredMoment(2, 3.14); } }); builder.registerTypeAdapter(CoefficientOfVariation.class, new InstanceCreator<CoefficientOfVariation>() { @Override public CoefficientOfVariation createInstance(Type arg0) { return new CoefficientOfVariation(Arrays.asList(new Double[] { 0.0 })); } }); builder.registerTypeAdapter(ConfidenceInterval.class, new InstanceCreator<ConfidenceInterval>() { @Override public ConfidenceInterval createInstance(Type arg0) { return new ConfidenceInterval(new Quantile(0.25, 0.1), new Quantile(0.75, 0.1)); } }); builder.registerTypeAdapter(ConfusionMatrix.class, new InstanceCreator<ConfusionMatrix>() { @Override public ConfusionMatrix createInstance(Type arg0) { String[] categories = new String[] { "OAK" }; return new ConfusionMatrix(categories, new int[] { 1 }); //return (ConfusionMatrix) new Randomizer().generate(ConfusionMatrix.class, 1); } }); builder.registerTypeAdapter(Correlation.class, new InstanceCreator<Correlation>() { @Override public Correlation createInstance(Type arg0) { return new Correlation(0.0); //return (Correlation) new Randomizer().generate(Correlation.class, 1); } }); builder.registerTypeAdapter(CovarianceMatrix.class, new InstanceCreator<CovarianceMatrix>() { @Override public CovarianceMatrix createInstance(Type arg0) { CovarianceMatrix cm = new CovarianceMatrix(2, new double[] { 3.14, 0, 0, 3.14 }); return cm; } }); builder.registerTypeAdapter(CredibleInterval.class, new InstanceCreator<CredibleInterval>() { @Override public CredibleInterval createInstance(Type arg0) { return new CredibleInterval(new Quantile(0.25, 0.1), new Quantile(0.75, 0.1)); } }); builder.registerTypeAdapter(Decile.class, new InstanceCreator<Decile>() { @Override public Decile createInstance(Type arg0) { return new Decile(5, 0.0); } }); builder.registerTypeAdapter(DiscreteProbability.class, new InstanceCreator<DiscreteProbability>() { @Override public DiscreteProbability createInstance(Type arg0) { return new DiscreteProbability("A", 0.0); } }); builder.registerTypeAdapter(InterquartileRange.class, new InstanceCreator<InterquartileRange>() { @Override public InterquartileRange createInstance(Type arg0) { return new InterquartileRange(0.0, 0.0); } }); builder.registerTypeAdapter(Kurtosis.class, new InstanceCreator<Kurtosis>() { @Override public Kurtosis createInstance(Type arg0) { return new Kurtosis(0.0); } }); builder.registerTypeAdapter(Mean.class, new InstanceCreator<Mean>() { @Override public Mean createInstance(Type arg0) { return new Mean(0.25); } }); builder.registerTypeAdapter(Median.class, new InstanceCreator<Median>() { @Override public Median createInstance(Type arg0) { return new Median(0.25); } }); builder.registerTypeAdapter(Mode.class, new InstanceCreator<Mode>() { @Override public Mode createInstance(Type arg0) { return new Mode(0.25); } }); builder.registerTypeAdapter(Moment.class, new InstanceCreator<Moment>() { @Override public Moment createInstance(Type arg0) { return new Moment(10, 0.25); } }); builder.registerTypeAdapter(ParetoDistribution.class, new InstanceCreator<ParetoDistribution>() { @Override public ParetoDistribution createInstance(Type arg0) { return new ParetoDistribution(0.5, 0.5); } }); builder.registerTypeAdapter(WishartDistribution.class, new InstanceCreator<WishartDistribution>() { @Override public WishartDistribution createInstance(Type arg0) { return new WishartDistribution(0.4, new CovarianceMatrix(2, new double[] { 1, 2 })); } }); builder.registerTypeAdapter(Percentile.class, new InstanceCreator<Percentile>() { @Override public Percentile createInstance(Type arg0) { return new Percentile(0.5, 0.5); } }); builder.registerTypeAdapter(Probability.class, new InstanceCreator<Probability>() { @Override public Probability createInstance(Type arg0) { return new Probability(new ProbabilityConstraint(ConstraintType.GREATER_OR_EQUAL, 0.5), 0.5); } }); builder.registerTypeAdapter(ProbabilityConstraint.class, new InstanceCreator<ProbabilityConstraint>() { @Override public ProbabilityConstraint createInstance(Type arg0) { return new ProbabilityConstraint(ConstraintType.GREATER_OR_EQUAL, 0.5); } }); builder.registerTypeAdapter(Quantile.class, new InstanceCreator<Quantile>() { @Override public Quantile createInstance(Type arg0) { return new Quantile(0.25, 0.1); } }); builder.registerTypeAdapter(Quartile.class, new InstanceCreator<Quartile>() { @Override public Quartile createInstance(Type arg0) { return new Quartile(0.25, 0.1); } }); builder.registerTypeAdapter(Range.class, new InstanceCreator<Range>() { @Override public Range createInstance(Type arg0) { return new Range(0.5, 0.5); } }); builder.registerTypeAdapter(Skewness.class, new InstanceCreator<Skewness>() { @Override public Skewness createInstance(Type arg0) { return new Skewness(0.5); } }); builder.registerTypeAdapter(StandardDeviation.class, new InstanceCreator<StandardDeviation>() { @Override public StandardDeviation createInstance(Type arg0) { return new StandardDeviation(0.5); } }); builder.registerTypeAdapter(Variance.class, new InstanceCreator<Variance>() { @Override public Variance createInstance(Type arg0) { return new Variance(0.5); } }); }
From source file:ts.internal.client.protocol.CompletionsRequest.java
License:Open Source License
@Override public Response<List<CompletionEntry>> parseResponse(JsonObject json) { String fileName = super.getArguments().getFile(); int line = super.getArguments().getLine(); int offset = super.getArguments().getOffset(); Gson gson = new GsonBuilder() .registerTypeAdapter(CompletionEntry.class, new InstanceCreator<CompletionEntry>() { @Override//from ww w . j a v a 2 s . c om public CompletionEntry createInstance(Type type) { return factory.create(matcherProvider.getMatcher(), fileName, line, offset, client); } }).create(); return gson.fromJson(json, CompletionsResponse.class); }
From source file:ts.internal.client.protocol.NavBarRequest.java
License:Open Source License
@Override public Response<List<NavigationBarItem>> parseResponse(JsonObject json) { Gson gson = GsonHelper.DEFAULT_GSON; if (positionProvider != null) { gson = new GsonBuilder().registerTypeAdapter(Location.class, new InstanceCreator<Location>() { @Override/*from w w w . j a v a2 s. c o m*/ public Location createInstance(Type type) { return new Location(positionProvider); } }).create(); } return gson.fromJson(json, NavBarResponse.class); }
From source file:ts.internal.client.protocol.NavTreeRequest.java
License:Open Source License
@Override public Response<NavigationBarItem> parseResponse(JsonObject json) { Gson gson = GsonHelper.DEFAULT_GSON; if (positionProvider != null) { gson = new GsonBuilder().registerTypeAdapter(Location.class, new InstanceCreator<Location>() { @Override//from w ww. j a va2s. c o m public Location createInstance(Type type) { return new Location(positionProvider); } }).create(); } return gson.fromJson(json, NavTreeResponse.class); }