/gson/src/main/java/com/google/gson/JsonParser.java

http://google-gson.googlecode.com/ · Java · 93 lines · 42 code · 6 blank · 45 comment · 3 complexity · 8cf5a70141f44c0817b0e912a8d2f929 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.gson;
  17. import java.io.IOException;
  18. import java.io.Reader;
  19. import java.io.StringReader;
  20. import com.google.gson.internal.Streams;
  21. import com.google.gson.stream.JsonReader;
  22. import com.google.gson.stream.JsonToken;
  23. import com.google.gson.stream.MalformedJsonException;
  24. /**
  25. * A parser to parse Json into a parse tree of {@link JsonElement}s
  26. *
  27. * @author Inderjeet Singh
  28. * @author Joel Leitch
  29. * @since 1.3
  30. */
  31. public final class JsonParser {
  32. /**
  33. * Parses the specified JSON string into a parse tree
  34. *
  35. * @param json JSON text
  36. * @return a parse tree of {@link JsonElement}s corresponding to the specified JSON
  37. * @throws JsonParseException if the specified text is not valid JSON
  38. * @since 1.3
  39. */
  40. public JsonElement parse(String json) throws JsonSyntaxException {
  41. return parse(new StringReader(json));
  42. }
  43. /**
  44. * Parses the specified JSON string into a parse tree
  45. *
  46. * @param json JSON text
  47. * @return a parse tree of {@link JsonElement}s corresponding to the specified JSON
  48. * @throws JsonParseException if the specified text is not valid JSON
  49. * @since 1.3
  50. */
  51. public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException {
  52. try {
  53. JsonReader jsonReader = new JsonReader(json);
  54. JsonElement element = parse(jsonReader);
  55. if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) {
  56. throw new JsonSyntaxException("Did not consume the entire document.");
  57. }
  58. return element;
  59. } catch (MalformedJsonException e) {
  60. throw new JsonSyntaxException(e);
  61. } catch (IOException e) {
  62. throw new JsonIOException(e);
  63. } catch (NumberFormatException e) {
  64. throw new JsonSyntaxException(e);
  65. }
  66. }
  67. /**
  68. * Returns the next value from the JSON stream as a parse tree.
  69. *
  70. * @throws JsonParseException if there is an IOException or if the specified
  71. * text is not valid JSON
  72. * @since 1.6
  73. */
  74. public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException {
  75. boolean lenient = json.isLenient();
  76. json.setLenient(true);
  77. try {
  78. return Streams.parse(json);
  79. } catch (StackOverflowError e) {
  80. throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e);
  81. } catch (OutOfMemoryError e) {
  82. throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e);
  83. } finally {
  84. json.setLenient(lenient);
  85. }
  86. }
  87. }