2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.rest.gson;
10 import com.google.gson.JsonArray;
11 import com.google.gson.JsonElement;
12 import com.google.gson.JsonIOException;
13 import com.google.gson.JsonNull;
14 import com.google.gson.JsonObject;
15 import com.google.gson.JsonParseException;
16 import com.google.gson.JsonPrimitive;
17 import com.google.gson.JsonSyntaxException;
18 import com.google.gson.internal.LazilyParsedNumber;
19 import com.google.gson.stream.JsonReader;
20 import com.google.gson.stream.MalformedJsonException;
21 import java.io.EOFException;
22 import java.io.IOException;
25 * @deprecated class will be removed in Lithium release
27 * This class parses JSON elements from a gson JsonReader. It disallows multiple elements of the same name unlike the
28 * default gson JsonParser."
31 public class JsonParser {
32 public JsonElement parse(final JsonReader reader) throws JsonIOException, JsonSyntaxException {
33 // code copied from gson's JsonParser and Stream classes
35 final boolean lenient = reader.isLenient();
36 reader.setLenient(true);
37 boolean isEmpty = true;
42 } catch (final EOFException e) {
44 return JsonNull.INSTANCE;
46 // The stream ended prematurely so it is likely a syntax error.
47 throw new JsonSyntaxException(e);
48 } catch (final MalformedJsonException e) {
49 throw new JsonSyntaxException(e);
50 } catch (final IOException e) {
51 throw new JsonIOException(e);
52 } catch (final NumberFormatException e) {
53 throw new JsonSyntaxException(e);
54 } catch (StackOverflowError | OutOfMemoryError e) {
55 throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e);
57 reader.setLenient(lenient);
61 public JsonElement read(final JsonReader in) throws IOException {
64 return new JsonPrimitive(in.nextString());
66 final String number = in.nextString();
67 return new JsonPrimitive(new LazilyParsedNumber(number));
69 return new JsonPrimitive(in.nextBoolean());
72 return JsonNull.INSTANCE;
74 final JsonArray array = new JsonArray();
76 while (in.hasNext()) {
82 final JsonObject object = new JsonObject();
84 while (in.hasNext()) {
85 final String childName = in.nextName();
86 if (object.has(childName)) {
87 throw new JsonSyntaxException("Duplicate name " + childName + " in JSON input.");
89 object.add(childName, read(in));
98 throw new IllegalArgumentException();