Skip to content

Commit 083abf9

Browse files
committed
docs: Document Spec.fromJson / fromYaml InputStream loaders
README only covered Spec.fromPath; add a classpath example using getResourceAsStream and document the BYO-parser overloads for callers that don't want a Gson or SnakeYAML dependency.
1 parent b1b6c66 commit 083abf9

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ public class YourServerLauncher {
125125

126126
`Spec.fromPath(Path)` picks the parser by file extension: `.json` is parsed by Gson, `.yaml` / `.yml` by SnakeYAML. Both are optional dependencies of this library — the same Gson that powers the built-in JSON `TypeMapper`, and the same SnakeYAML you'd add explicitly to parse YAML. If the required parser isn't on the classpath the call fails with `IllegalStateException`; parse the file yourself and use `Spec.from(Map<String, Object>)` instead. Any other extension is rejected.
127127

128+
To load a spec from the classpath (including from inside a JAR) use the `InputStream` overloads:
129+
130+
``` java
131+
try (InputStream in = YourServerLauncher.class.getResourceAsStream("/openapi.json")) {
132+
Spec spec = Spec.fromJson(in); // Gson on the classpath
133+
}
134+
```
135+
136+
The matching `Spec.fromYaml(InputStream)` uses SnakeYAML. Both close the stream before returning. If you can't (or don't want to) depend on Gson/SnakeYAML, supply your own parser:
137+
138+
``` java
139+
ObjectMapper jackson = new ObjectMapper();
140+
try (InputStream in = YourServerLauncher.class.getResourceAsStream("/openapi.json")) {
141+
Spec spec = Spec.fromJson(in, bytes -> jackson.readValue(bytes, Map.class));
142+
}
143+
```
144+
145+
`Spec.fromYaml(InputStream, Function<byte[], Map<String, Object>>)` is the equivalent escape hatch for YAML.
146+
128147
### JSON mapping
129148

130149
The library ships an internal `GsonJsonMapper` that is auto-registered for `application/json` when Gson is on the classpath and no user-supplied JSON mapper has been registered. It:

0 commit comments

Comments
 (0)