• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #19514

17 Oct 2024 06:11PM UTC coverage: 84.69% (+0.05%) from 84.645%
#19514

push

github

web-flow
core: SpiffeUtil API for extracting Spiffe URI and loading TrustBundles (#11575)

Additional API for SpiffeUtil:
 - extract Spiffe URI from certificate chain
 - load Spiffe Trust Bundle from filesystem [json spec][] [JWK spec][]

JsonParser was changed to reject duplicate keys in objects.

[json spec]: https://github.com/spiffe/spiffe/blob/main/standards/SPIFFE_Trust_Domain_and_Bundle.md
[JWK spec]: https://github.com/spiffe/spiffe/blob/main/standards/X509-SVID.md#61-publishing-spiffe-bundle-elements

33877 of 40001 relevant lines covered (84.69%)

0.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.11
/../core/src/main/java/io/grpc/internal/JsonParser.java
1
/*
2
 * Copyright 2018 The gRPC Authors
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

17
package io.grpc.internal;
18

19
import static com.google.common.base.Preconditions.checkArgument;
20
import static com.google.common.base.Preconditions.checkState;
21

22
import com.google.gson.stream.JsonReader;
23
import com.google.gson.stream.JsonToken;
24
import java.io.IOException;
25
import java.io.StringReader;
26
import java.util.ArrayList;
27
import java.util.Collections;
28
import java.util.LinkedHashMap;
29
import java.util.List;
30
import java.util.Map;
31
import java.util.logging.Level;
32
import java.util.logging.Logger;
33

34
/**
35
 * Parses JSON with as few preconceived notions as possible.
36
 */
37
public final class JsonParser {
38

39
  private static final Logger logger = Logger.getLogger(JsonParser.class.getName());
1✔
40

41
  private JsonParser() {}
42

43
  /**
44
   * Parses a json string, returning either a {@code Map<String, ?>}, {@code List<?>},
45
   * {@code String}, {@code Double}, {@code Boolean}, or {@code null}. Fails if duplicate names
46
   * found.
47
   */
48
  public static Object parse(String raw) throws IOException {
49
    JsonReader jr = new JsonReader(new StringReader(raw));
1✔
50
    try {
51
      return parseRecursive(jr);
1✔
52
    } finally {
53
      try {
54
        jr.close();
1✔
55
      } catch (IOException e) {
×
56
        logger.log(Level.WARNING, "Failed to close", e);
×
57
      }
1✔
58
    }
59
  }
60

61
  private static Object parseRecursive(JsonReader jr) throws IOException {
62
    checkState(jr.hasNext(), "unexpected end of JSON");
1✔
63
    switch (jr.peek()) {
1✔
64
      case BEGIN_ARRAY:
65
        return parseJsonArray(jr);
1✔
66
      case BEGIN_OBJECT:
67
        return parseJsonObject(jr);
1✔
68
      case STRING:
69
        return jr.nextString();
1✔
70
      case NUMBER:
71
        return jr.nextDouble();
1✔
72
      case BOOLEAN:
73
        return jr.nextBoolean();
1✔
74
      case NULL:
75
        return parseJsonNull(jr);
1✔
76
      default:
77
        throw new IllegalStateException("Bad token: " + jr.getPath());
×
78
    }
79
  }
80

81
  private static Map<String, ?> parseJsonObject(JsonReader jr) throws IOException {
82
    jr.beginObject();
1✔
83
    Map<String, Object> obj = new LinkedHashMap<>();
1✔
84
    while (jr.hasNext()) {
1✔
85
      String name = jr.nextName();
1✔
86
      checkArgument(!obj.containsKey(name), "Duplicate key found: %s", name);
1✔
87
      Object value = parseRecursive(jr);
1✔
88
      obj.put(name, value);
1✔
89
    }
1✔
90
    checkState(jr.peek() == JsonToken.END_OBJECT, "Bad token: " + jr.getPath());
1✔
91
    jr.endObject();
1✔
92
    return Collections.unmodifiableMap(obj);
1✔
93
  }
94

95
  private static List<?> parseJsonArray(JsonReader jr) throws IOException {
96
    jr.beginArray();
1✔
97
    List<Object> array = new ArrayList<>();
1✔
98
    while (jr.hasNext()) {
1✔
99
      Object value = parseRecursive(jr);
1✔
100
      array.add(value);
1✔
101
    }
1✔
102
    checkState(jr.peek() == JsonToken.END_ARRAY, "Bad token: " + jr.getPath());
1✔
103
    jr.endArray();
1✔
104
    return Collections.unmodifiableList(array);
1✔
105
  }
106

107
  private static Void parseJsonNull(JsonReader jr) throws IOException {
108
    jr.nextNull();
1✔
109
    return null;
1✔
110
  }
111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc