1414use cebe \openapi \json \JsonPointer ;
1515use cebe \openapi \spec \OpenApi ;
1616use Symfony \Component \Yaml \Yaml ;
17+ use JsonStreamingParser \Parser ;
18+ use JsonStreamingParser \Listener \InMemoryListener ;
1719
1820/**
1921 * Utility class to simplify reading JSON or YAML OpenAPI specs.
@@ -34,9 +36,17 @@ class Reader
3436 * The type of the returned object depends on the `$baseType` argument.
3537 * @throws TypeErrorException in case invalid spec data is supplied.
3638 */
37- public static function readFromJson (string $ json , string $ baseType = OpenApi::class): SpecObjectInterface
39+ public static function readFromJson (string $ filename , string $ baseType = OpenApi::class): SpecObjectInterface
3840 {
39- return new $ baseType (json_decode ($ json , true ));
41+ $ fp = fopen ($ filename , 'r ' );
42+ $ listener = new InMemoryListener ();
43+ $ parser = new Parser ($ fp , $ listener );
44+ $ parser ->parse ();
45+ fclose ($ fp );
46+
47+ $ data = $ listener ->getJson ();
48+
49+ return new $ baseType ($ data );
4050 }
4151
4252 /**
@@ -83,21 +93,15 @@ public static function readFromYaml(string $yaml, string $baseType = OpenApi::cl
8393 */
8494 public static function readFromJsonFile (string $ fileName , string $ baseType = OpenApi::class, $ resolveReferences = true ): SpecObjectInterface
8595 {
86- $ fileContent = file_get_contents ($ fileName );
87- if ($ fileContent === false ) {
88- $ e = new IOException ("Failed to read file: ' $ fileName' " );
89- $ e ->fileName = $ fileName ;
90- throw $ e ;
91- }
92- $ spec = static ::readFromJson ($ fileContent , $ baseType );
96+ $ spec = static ::readFromJson ($ fileName , $ baseType );
9397 $ context = new ReferenceContext ($ spec , $ fileName );
9498 $ spec ->setReferenceContext ($ context );
9599 if ($ resolveReferences !== false ) {
96100 if (is_string ($ resolveReferences )) {
97101 $ context ->mode = $ resolveReferences ;
98102 }
99103 if ($ spec instanceof DocumentContextInterface) {
100- $ spec ->setDocumentContext ($ spec , new JsonPointer ('' ));
104+ // $spec->setDocumentContext($spec, new JsonPointer(''));
101105 }
102106 $ spec ->resolveReferences ();
103107 }
0 commit comments