BUG 2412 - Restconf migration - marking deprecated classes
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / gson / JsonParser.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.rest.gson;
9
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;
23
24 /**
25  * @deprecated class will be removed in Lithium release
26  *
27  * This class parses JSON elements from a gson JsonReader. It disallows multiple elements of the same name unlike the
28  * default gson JsonParser."
29  */
30 @Deprecated
31 public class JsonParser {
32     public JsonElement parse(final JsonReader reader) throws JsonIOException, JsonSyntaxException {
33         // code copied from gson's JsonParser and Stream classes
34
35         final boolean lenient = reader.isLenient();
36         reader.setLenient(true);
37         boolean isEmpty = true;
38         try {
39             reader.peek();
40             isEmpty = false;
41             return read(reader);
42         } catch (final EOFException e) {
43             if (isEmpty) {
44                 return JsonNull.INSTANCE;
45             }
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);
56         } finally {
57             reader.setLenient(lenient);
58         }
59     }
60
61     public JsonElement read(final JsonReader in) throws IOException {
62         switch (in.peek()) {
63         case STRING:
64             return new JsonPrimitive(in.nextString());
65         case NUMBER:
66             final String number = in.nextString();
67             return new JsonPrimitive(new LazilyParsedNumber(number));
68         case BOOLEAN:
69             return new JsonPrimitive(in.nextBoolean());
70         case NULL:
71             in.nextNull();
72             return JsonNull.INSTANCE;
73         case BEGIN_ARRAY:
74             final JsonArray array = new JsonArray();
75             in.beginArray();
76             while (in.hasNext()) {
77                 array.add(read(in));
78             }
79             in.endArray();
80             return array;
81         case BEGIN_OBJECT:
82             final JsonObject object = new JsonObject();
83             in.beginObject();
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.");
88                 }
89                 object.add(childName, read(in));
90             }
91             in.endObject();
92             return object;
93         case END_DOCUMENT:
94         case NAME:
95         case END_OBJECT:
96         case END_ARRAY:
97         default:
98             throw new IllegalArgumentException();
99         }
100     }
101 }