List of usage examples for com.google.gson JsonElement getAsDouble
public double getAsDouble()
From source file:org.ssc.txtproc.TextProcessor.java
License:Open Source License
private List<Double> getAsDoubleArray(JsonElement element) { if (element == null) return null; JsonArray childElems = element.getAsJsonArray(); if (childElems == null) return null; ArrayList<Double> values = new ArrayList<Double>(childElems.size()); for (JsonElement elem : childElems) { values.add(new Double(elem.getAsDouble())); }/* ww w . jav a 2 s . c o m*/ return values; }
From source file:org.structr.core.PropertySetGSONAdapter.java
License:Open Source License
private Object getTypedValue(JsonElement valueElement, String type) { Object value = null;/*from w ww .ja va 2s .c o m*/ if ((type == null) || type.equals("null")) { value = valueElement.getAsJsonNull(); } else if (type.equals("String")) { value = valueElement.getAsString(); } else if (type.equals("Number")) { value = valueElement.getAsNumber(); } else if (type.equals("Boolean")) { value = valueElement.getAsBoolean(); } else if (type.equals("JsonArray")) { value = valueElement.getAsJsonArray(); } else if (type.equals("JsonObject")) { value = valueElement.getAsJsonObject(); } else if (type.equals("Integer")) { value = valueElement.getAsInt(); } else if (type.equals("Long")) { value = valueElement.getAsLong(); } else if (type.equals("Double")) { value = valueElement.getAsDouble(); } else if (type.equals("Float")) { value = valueElement.getAsFloat(); } else if (type.equals("Byte")) { value = valueElement.getAsByte(); } else if (type.equals("Short")) { value = valueElement.getAsShort(); } else if (type.equals("Character")) { value = valueElement.getAsCharacter(); } else if (type.equals("BigDecimal")) { value = valueElement.getAsBigDecimal(); } else if (type.equals("BigInteger")) { value = valueElement.getAsBigInteger(); } return value; }
From source file:org.terasology.persistence.typeHandling.gson.GsonPersistedDataArray.java
License:Apache License
@Override public TDoubleList getAsDoubleArray() { TDoubleList result = new TDoubleArrayList(size()); for (JsonElement element : array) { result.add(element.getAsDouble()); }/*from w ww .jav a 2 s .co m*/ return result; }
From source file:org.uorm.serializer.DefaultJsonSerializer.java
License:Apache License
@Override public Map<String, Object> deserialize2(Class<?> cls, String json) throws Exception { JsonStreamParser parser = new JsonStreamParser(json); if (parser.hasNext()) { JsonObject jsonobj = parser.next().getAsJsonObject(); Set<Entry<String, JsonElement>> jset = jsonobj.entrySet(); if (!jset.isEmpty()) { Map<String, PropertyDescriptor> propMap = ObjectMappingCache.getInstance() .getObjectPropertyMap(cls); Map<String, Object> instance = new HashMap<String, Object>(); for (Entry<String, JsonElement> entry : jset) { String name = entry.getKey(); JsonElement val = entry.getValue(); if (!val.isJsonNull()) { PropertyDescriptor descriptor = propMap.get(name); if (descriptor != null) { Class<?> memberType = descriptor.getPropertyType(); if (memberType == String.class) { instance.put(name, val.getAsString()); } else if (memberType == Integer.class || memberType == Integer.TYPE) { instance.put(name, val.getAsInt()); } else if (memberType == Byte.class || memberType == Byte.TYPE) { instance.put(name, val.getAsByte()); } else if (memberType == Double.class || memberType == Double.TYPE) { instance.put(name, val.getAsDouble()); } else if (memberType == Float.class || memberType == Float.TYPE) { instance.put(name, val.getAsFloat()); } else if (memberType == Long.class || memberType == Long.TYPE) { instance.put(name, val.getAsLong()); } else if (memberType == Short.class || memberType == Short.TYPE) { instance.put(name, val.getAsShort()); } else if (memberType == Boolean.class || memberType == Boolean.TYPE) { instance.put(name, val.getAsBoolean()); } else if (memberType == BigDecimal.class) { instance.put(name, val.getAsBigDecimal()); } else if (memberType == BigInteger.class) { instance.put(name, val.getAsBigInteger()); } else if (memberType == byte[].class) { instance.put(name, val.getAsString().getBytes()); } else if (memberType == java.sql.Timestamp.class) { Long time = parserDate(val.getAsString()); if (time == null) { instance.put(name, null); } else { instance.put(name, new java.sql.Timestamp(time)); }/*from ww w . j a va 2s. com*/ } else if (memberType == java.sql.Date.class) { Long time = parserDate(val.getAsString()); if (time == null) { instance.put(name, null); } else { instance.put(name, new java.sql.Date(time)); } } else if (memberType == java.util.Date.class) { Long time = parserDate(val.getAsString()); if (time == null) { instance.put(name, null); } else { instance.put(name, new java.util.Date(time)); } } else {//default String instance.put(name, val.getAsString()); } } else {//String instance.put(name, val.getAsString()); } } } return instance; } } return null; }
From source file:org.waveprotocol.wave.diff.proto.DocumentDiffSnapshotProtoImpl.java
License:Apache License
@Override public void fromGson(JsonElement json, Gson gson, RawStringData raw) throws GsonException { JsonObject jsonObject = json.getAsJsonObject(); // NOTE: always check with has(...) as the json might not have all required // fields set; however these (obviously) will need to be set by other means // before accessing this object. invalidateAll();//w ww . ja v a 2s . com /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("1")) { JsonElement elem = jsonObject.get("1"); setDocumentId(elem.getAsString()); } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("2")) { JsonElement elem = jsonObject.get("2"); if (!elem.isJsonNull()) { { ProtocolDocumentOperationProtoImpl payload = new ProtocolDocumentOperationProtoImpl(); GsonUtil.extractJsonObject(payload, elem, gson, raw); setState(payload); } } } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("21")) { JsonElement elem = jsonObject.get("21"); if (!elem.isJsonNull()) { { ProtocolDocumentOperationProtoImpl payload = new ProtocolDocumentOperationProtoImpl(); GsonUtil.extractJsonObject(payload, elem, gson, raw); setDiff(payload); } } } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("3")) { JsonElement elem = jsonObject.get("3"); setAuthor(elem.getAsString()); } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("4")) { JsonElement elem = jsonObject.get("4"); { JsonArray array = elem.getAsJsonArray(); for (int i = 0; i < array.size(); i++) { addContributor(array.get(i).getAsString()); } } } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("22")) { JsonElement elem = jsonObject.get("22"); { JsonArray array = elem.getAsJsonArray(); for (int i = 0; i < array.size(); i++) { addAddedContributor(array.get(i).getAsString()); } } } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("23")) { JsonElement elem = jsonObject.get("23"); { JsonArray array = elem.getAsJsonArray(); for (int i = 0; i < array.size(); i++) { addRemovedContributor(array.get(i).getAsString()); } } } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("5")) { JsonElement elem = jsonObject.get("5"); setLastModifiedVersion(elem.getAsDouble()); } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("6")) { JsonElement elem = jsonObject.get("6"); setLastModifiedTime(elem.getAsDouble()); } }
From source file:org.waveprotocol.wave.diff.proto.WaveletDiffSnapshotProtoImpl.java
License:Apache License
@Override public void fromGson(JsonElement json, Gson gson, RawStringData raw) throws GsonException { JsonObject jsonObject = json.getAsJsonObject(); // NOTE: always check with has(...) as the json might not have all required // fields set; however these (obviously) will need to be set by other means // before accessing this object. invalidateAll();//from ww w. j ava 2 s . co m /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("1")) { JsonElement elem = jsonObject.get("1"); setWaveletId(elem.getAsString()); } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("2")) { JsonElement elem = jsonObject.get("2"); { JsonArray array = elem.getAsJsonArray(); for (int i = 0; i < array.size(); i++) { addParticipant(array.get(i).getAsString()); } } } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("21")) { JsonElement elem = jsonObject.get("21"); { JsonArray array = elem.getAsJsonArray(); for (int i = 0; i < array.size(); i++) { addAddedParticipant(array.get(i).getAsString()); } } } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("22")) { JsonElement elem = jsonObject.get("22"); { JsonArray array = elem.getAsJsonArray(); for (int i = 0; i < array.size(); i++) { addRemovedParticipant(array.get(i).getAsString()); } } } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("3")) { JsonElement elem = jsonObject.get("3"); { JsonArray array = elem.getAsJsonArray(); for (int i = 0; i < array.size(); i++) { DocumentDiffSnapshotProtoImpl payload = new DocumentDiffSnapshotProtoImpl(); GsonUtil.extractJsonObject(payload, array.get(i), gson, raw); addDocument(payload); } } } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("4")) { JsonElement elem = jsonObject.get("4"); { ProtocolHashedVersionProtoImpl payload = new ProtocolHashedVersionProtoImpl(); GsonUtil.extractJsonObject(payload, elem, gson, raw); setVersion(payload); } } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("5")) { JsonElement elem = jsonObject.get("5"); setLastModifiedTime(elem.getAsDouble()); } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("6")) { JsonElement elem = jsonObject.get("6"); setCreator(elem.getAsString()); } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("7")) { JsonElement elem = jsonObject.get("7"); setCreationTime(elem.getAsDouble()); } }
From source file:org.waveprotocol.wave.federation.proto.ProtocolAppliedWaveletDeltaProtoImpl.java
License:Apache License
@Override public void fromGson(JsonElement json, Gson gson, RawStringData raw) throws GsonException { JsonObject jsonObject = json.getAsJsonObject(); // NOTE: always check with has(...) as the json might not have all required // fields set; however these (obviously) will need to be set by other means // before accessing this object. invalidateAll();/*from w w w. ja v a 2s. co m*/ /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("1")) { JsonElement elem = jsonObject.get("1"); { ProtocolSignedDeltaProtoImpl payload = new ProtocolSignedDeltaProtoImpl(); GsonUtil.extractJsonObject(payload, elem, gson, raw); setSignedOriginalDelta(payload); } } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("2")) { JsonElement elem = jsonObject.get("2"); if (!elem.isJsonNull()) { { ProtocolHashedVersionProtoImpl payload = new ProtocolHashedVersionProtoImpl(); GsonUtil.extractJsonObject(payload, elem, gson, raw); setHashedVersionAppliedAt(payload); } } } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("3")) { JsonElement elem = jsonObject.get("3"); setOperationsApplied(elem.getAsInt()); } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("4")) { JsonElement elem = jsonObject.get("4"); setApplicationTimestamp(elem.getAsDouble()); } }
From source file:org.waveprotocol.wave.federation.proto.ProtocolHashedVersionProtoImpl.java
License:Apache License
@Override public void fromGson(JsonElement json, Gson gson, RawStringData raw) throws GsonException { JsonObject jsonObject = json.getAsJsonObject(); // NOTE: always check with has(...) as the json might not have all required // fields set; however these (obviously) will need to be set by other means // before accessing this object. invalidateAll();//www . ja va 2 s. c om /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("1")) { JsonElement elem = jsonObject.get("1"); setVersion(elem.getAsDouble()); } /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (jsonObject.has("2")) { JsonElement elem = jsonObject.get("2"); setHistoryHash(new Blob(elem.getAsString())); } }
From source file:org.zoxweb.server.util.GSONUtil.java
License:Apache License
@SuppressWarnings("unchecked") private static NVEntity fromJSON(JsonObject jo, Class<? extends NVEntity> clazz, Base64Type b64Type) throws AccessException, APIException { // check if the jo has class name setup // before creating the new instance JsonElement classType = jo.get(MetaToken.CLASS_TYPE.getName()); if (classType != null) { if (!classType.isJsonNull()) { try { clazz = (Class<? extends NVEntity>) Class.forName(classType.getAsString()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block //e.printStackTrace(); throw new APIException(e.getMessage(), Reason.NOT_FOUND); }/*w w w .j ava2 s.c o m*/ } } NVEntity nve = null; try { try { nve = clazz.getDeclaredConstructor().newInstance(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block //e.printStackTrace(); throw new APIException(e.getMessage(), Reason.NOT_FOUND); } } catch (InstantiationException | InvocationTargetException | NoSuchMethodException ie) { ie.printStackTrace(); log.info("Error class:" + clazz); log.info("" + jo.toString()); throw new APIException(ie.getMessage(), Reason.NOT_FOUND); // if (ie instanceof InstantiationException) // throw (InstantiationException)ie; // else // throw new InstantiationException(ie.getMessage()); } catch (SecurityException ie) { throw new AccessException(ie.getMessage(), Reason.ACCESS_DENIED); } if (jo.get(MetaToken.REFERENCE_ID.getName()) != null && !jo.get(MetaToken.REFERENCE_ID.getName()).isJsonNull()) { nve.setReferenceID(jo.get(MetaToken.REFERENCE_ID.getName()).getAsString()); } NVConfigEntity mcEntity = (NVConfigEntity) nve.getNVConfig(); List<NVConfig> nvconfigs = mcEntity.getAttributes(); for (NVConfig nvc : nvconfigs) { Class<?> metaType = nvc.getMetaType(); JsonElement je = jo.get(nvc.getName()); if (je != null && !je.isJsonNull()) { NVBase<?> nvb = nve.lookup(nvc.getName()); if (nvc.isArray()) { //if ( nvb instanceof NVBase<List<NVEntity>>) //if ( NVEntity.class.isAssignableFrom( metaType.getComponentType())) if (NVEntity.class.isAssignableFrom(nvc.getMetaTypeBase())) { ArrayValues<NVEntity> tempArray = (ArrayValues<NVEntity>) nvb; JsonArray jsonArray = je.getAsJsonArray(); for (int i = 0; i < jsonArray.size(); i++) { JsonObject jobj = jsonArray.get(i).getAsJsonObject(); // try { tempArray.add( fromJSON(jobj, (Class<? extends NVEntity>) nvc.getMetaTypeBase(), b64Type)); } // catch (InstantiationException ie) // { // log.info("nvc:" + nvc.getName() + ":" + nvc.getMetaTypeBase()); // throw ie; // } //nvl.getValue().add( toNVPair( jobj)); } } // enum must be checked first else if (metaType.getComponentType().isEnum()) { JsonArray jsonArray = je.getAsJsonArray(); NVBase<List<Enum<?>>> nel = (NVBase<List<Enum<?>>>) nvb; for (int i = 0; i < jsonArray.size(); i++) { String jobj = jsonArray.get(i).getAsString(); nel.getValue().add(SharedUtil.enumValue(metaType.getComponentType(), jobj)); } } else if (String[].class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); ArrayValues<NVPair> nvpm = (ArrayValues<NVPair>) nvb; for (int i = 0; i < jsonArray.size(); i++) { JsonObject jobj = jsonArray.get(i).getAsJsonObject(); nvpm.add(toNVPair(jobj)); } } else if (Long[].class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); NVBase<ArrayList<Long>> nval = (NVBase<ArrayList<Long>>) nvb; for (int i = 0; i < jsonArray.size(); i++) { nval.getValue().add(jsonArray.get(i).getAsLong()); } } else if (byte[].class.equals(metaType)) { String byteArray64 = je.getAsString(); if (byteArray64 != null) { nve.setValue(nvc, SharedBase64.decode(b64Type, byteArray64.getBytes())); } } else if (Integer[].class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); NVBase<ArrayList<Integer>> nval = (NVBase<ArrayList<Integer>>) nvb; for (int i = 0; i < jsonArray.size(); i++) { nval.getValue().add((int) jsonArray.get(i).getAsLong()); } } else if (Float[].class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); NVBase<ArrayList<Float>> nval = (NVBase<ArrayList<Float>>) nvb; for (int i = 0; i < jsonArray.size(); i++) { nval.getValue().add((float) jsonArray.get(i).getAsDouble()); } } else if (Double[].class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); NVBase<ArrayList<Double>> nval = (NVBase<ArrayList<Double>>) nvb; for (int i = 0; i < jsonArray.size(); i++) { nval.getValue().add(jsonArray.get(i).getAsDouble()); } } else if (Date[].class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); NVBase<ArrayList<Long>> nval = (NVBase<ArrayList<Long>>) nvb; for (int i = 0; i < jsonArray.size(); i++) { JsonPrimitive jp = (JsonPrimitive) jsonArray.get(i); long tempDate = 0; if (jp.isString() && nvc.getValueFilter() != null) { tempDate = (Long) nvc.getValueFilter().validate(jp.getAsString()); } else { tempDate = jp.getAsLong(); } nval.getValue().add(tempDate); } } else if (BigDecimal[].class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); NVBase<ArrayList<BigDecimal>> nval = (NVBase<ArrayList<BigDecimal>>) nvb; for (int i = 0; i < jsonArray.size(); i++) { nval.getValue().add(jsonArray.get(i).getAsBigDecimal()); } } } else { // not array if (nvc instanceof NVConfigEntity) { if (!(je instanceof JsonNull)) { ((NVBase<NVEntity>) nvb).setValue(fromJSON(je.getAsJsonObject(), (Class<? extends NVEntity>) nvc.getMetaType(), b64Type)); } } else if (NVGenericMap.class.equals(metaType)) { if (!(je instanceof JsonNull)) { NVGenericMap nvgm = fromJSONGenericMap(je.getAsJsonObject(), null, b64Type); ((NVGenericMap) nve.lookup(nvc)).add(nvgm.values(), true); } } else if (NVStringList.class.equals(metaType)) { JsonArray jsonArray = je.getAsJsonArray(); NVStringList nval = (NVStringList) nvb; for (int i = 0; i < jsonArray.size(); i++) { nval.getValue().add(jsonArray.get(i).getAsString()); } } else if (nvc.isEnum()) { if (!(je instanceof JsonNull)) { // if (metaType.isAssignableFrom( DynamicEnumMap.class)) // { // // ((NVDynamicEnum)nvb).setValue(je.getAsString()); // } // else { ((NVBase<Enum<?>>) nvb).setValue(SharedUtil.enumValue(metaType, je.getAsString())); } } } else if (String.class.equals(metaType)) { if (!(je instanceof JsonNull)) { ((NVPair) nvb).setValue(je.getAsString()); } } else if (Long.class.equals(metaType)) { ((NVBase<Long>) nvb).setValue(je.getAsLong()); } else if (Boolean.class.equals(metaType)) { ((NVBase<Boolean>) nvb).setValue(je.getAsBoolean()); } else if (Integer.class.equals(metaType)) { ((NVBase<Integer>) nvb).setValue((int) je.getAsLong()); } else if (Float.class.equals(metaType)) { ((NVBase<Float>) nvb).setValue((float) je.getAsDouble()); } else if (Double.class.equals(metaType)) { ((NVBase<Double>) nvb).setValue(je.getAsDouble()); } else if (Date.class.equals(metaType)) { JsonPrimitive jp = (JsonPrimitive) je; if (jp.isString()) { if (nvc.getValueFilter() != null) ((NVBase<Long>) nvb) .setValue((Long) nvc.getValueFilter().validate(jp.getAsString())); else ((NVBase<Long>) nvb).setValue(TimestampFilter.SINGLETON.validate(jp.getAsString())); } else { ((NVBase<Long>) nvb).setValue(jp.getAsLong()); } } else if (BigDecimal.class.equals(metaType)) { ((NVBase<BigDecimal>) nvb).setValue(je.getAsBigDecimal()); } } } } if (nve instanceof SubjectID) { ((SubjectID<?>) nve).getSubjectID(); } return nve; }
From source file:utils.FeedUtils.java
License:Open Source License
/** * Get a Java date from a JsonElement with Unix time. * @param e//from ww w . j ava 2s .c o m * @return */ private static Date getDateFromJson(JsonElement e) { long date = ((long) e.getAsDouble()) * 1000L; return new Date(date); }