@@ -862,40 +862,48 @@ export function parseJsonAst(input: string, mode = JsonParseMode.Default): JsonA
862
862
863
863
864
864
/**
865
- * Parse a JSON string into its value. This discards the AST and only returns the value itself.
866
- * @param input The string to parse.
867
- * @param mode The mode to parse the input with. {@see JsonParseMode}.
868
- * @returns {JsonValue } The value represented by the JSON string.
865
+ * Options for the parseJson() function.
869
866
*/
870
- export function parseJson ( input : string , mode = JsonParseMode . Default ) : JsonValue {
871
- // Try parsing for the fastest path available, if error, uses our own parser for better errors.
872
- if ( mode == JsonParseMode . Strict ) {
873
- try {
874
- return JSON . parse ( input ) ;
875
- } catch ( err ) {
876
- return parseJsonAst ( input , mode ) . value ;
877
- }
878
- }
879
-
880
- return parseJsonAst ( input , mode ) . value ;
867
+ interface ParseJsonOptions {
868
+ /**
869
+ * If omitted, will only emit errors related to the content of the JSON. If specified, any
870
+ * JSON errors will also include the path of the file that caused the error.
871
+ */
872
+ path ?: string ;
881
873
}
882
874
875
+
883
876
/**
884
877
* Parse a JSON string into its value. This discards the AST and only returns the value itself.
885
- * It also absorbs JSON parsing errors and return a new error with the path in it. Useful for
886
- * showing errors when parsing from a file.
878
+ *
879
+ * If a path option is pass, it also absorbs JSON parsing errors and return a new error with the
880
+ * path in it. Useful for showing errors when parsing from a file.
881
+ *
887
882
* @param input The string to parse.
888
883
* @param mode The mode to parse the input with. {@see JsonParseMode}.
884
+ * @param options Additional optinos for parsing.
889
885
* @returns {JsonValue } The value represented by the JSON string.
890
886
*/
891
- export function parseJsonFile ( input : string , mode = JsonParseMode . Default , path : string ) {
887
+ export function parseJson (
888
+ input : string ,
889
+ mode = JsonParseMode . Default ,
890
+ options ?: ParseJsonOptions ,
891
+ ) : JsonValue {
892
892
try {
893
- return parseJson ( input , mode ) ;
893
+ // Try parsing for the fastest path available, if error, uses our own parser for better errors.
894
+ if ( mode == JsonParseMode . Strict ) {
895
+ try {
896
+ return JSON . parse ( input ) ;
897
+ } catch ( err ) {
898
+ return parseJsonAst ( input , mode ) . value ;
899
+ }
900
+ }
901
+
902
+ return parseJsonAst ( input , mode ) . value ;
894
903
} catch ( e ) {
895
- if ( e instanceof JsonException ) {
896
- throw new PathSpecificJsonException ( path , e ) ;
897
- } else {
898
- throw e ;
904
+ if ( options && options . path && e instanceof JsonException ) {
905
+ throw new PathSpecificJsonException ( options . path , e ) ;
899
906
}
907
+ throw e ;
900
908
}
901
909
}
0 commit comments