Debian Codegraph @

net/sf/saxon/ma/json/ParseJsonFn.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.ma.json;
9
10 import net.sf.saxon.expr.StaticProperty;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.functions.OptionsParameter;
13 import net.sf.saxon.ma.map.MapItem;
14 import net.sf.saxon.om.Item;
15 import net.sf.saxon.om.Sequence;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.type.SpecificFunctionType;
18 import net.sf.saxon.value.BooleanValue;
19 import net.sf.saxon.value.SequenceType;
20 import net.sf.saxon.value.StringValue;
21
22 import java.util.Map;
23
24 /**
25 * Implements the parse-json function, as defined in XPath 3.1
26 *
27 * The parsing code, and the handling of options is shared with the json-to-xml function.
28 */
29 public class ParseJsonFn extends JsonToXMLFn {
30
31 public static OptionsParameter OPTION_DETAILS;
32 static {
33 SpecificFunctionType fallbackType = new SpecificFunctionType(
34 new SequenceType[]{SequenceType.SINGLE_STRING}, SequenceType.SINGLE_STRING);
35 OptionsParameter parseJsonOptions = new OptionsParameter();
36 parseJsonOptions.addAllowedOption("liberal", SequenceType.SINGLE_BOOLEAN, BooleanValue.FALSE);
37 parseJsonOptions.addAllowedOption("duplicates", SequenceType.SINGLE_STRING, new StringValue("use-first"));
38 parseJsonOptions.setAllowedValues("duplicates", "FOJS0005", "reject", "use-first", "use-last");
39 parseJsonOptions.addAllowedOption("escape", SequenceType.SINGLE_BOOLEAN, BooleanValue.FALSE);
40 parseJsonOptions.addAllowedOption("fallback", SequenceType.makeSequenceType(fallbackType, StaticProperty.EXACTLY_ONE), null);
41 OPTION_DETAILS = parseJsonOptions;
42 }
43
44
45 /**
46 * Parse the JSON string according to supplied options
47 *
48 * @param input JSON input string
49 * @param options options for the conversion as a map of xs:string : value pairs
50 * @param context XPath evaluation context
51 * @return the result of the parsing, as an XML element
52 * @throws XPathException if the syntax of the input is incorrect
53 */
54 @Override
55 protected Item eval(String input, MapItem options, XPathContext context) throws XPathException {
56 Map<String, Sequence<?>> checkedOptions = null;
57 if (options != null) {
58 checkedOptions = getDetails().optionDetails.processSuppliedOptions(options, context);
59 }
60 return parse(input, checkedOptions, context);
61 }
62
63 /**
64 * Parse the JSON string according to supplied options
65 *
66 * @param input JSON input string
67 * @param options options for the conversion as a map of xs:string : value pairs
68 * @param context XPath evaluation context
69 * @return the result of the parsing, as an XML element
70 * @throws XPathException if the syntax of the input is incorrect
71 */
72
73 public static Item parse(String input, Map<String, Sequence<?>> options, XPathContext context) throws XPathException {
74 JsonParser parser = new JsonParser();
75 int flags = 0;
76 if (options != null) {
77 flags = JsonParser.getFlags(options, context, false);
78 }
79 JsonHandlerMap handler = new JsonHandlerMap(context, flags);
80 if ((flags & JsonParser.DUPLICATES_RETAINED) != 0) {
81 throw new XPathException("parse-json: duplicates=retain is not allowed", "FOJS0005");
82 }
83 if ((flags & JsonParser.DUPLICATES_SPECIFIED) == 0) {
84 flags |= JsonParser.DUPLICATES_FIRST;
85 }
86 if (options != null) {
87 handler.setFallbackFunction(options, context);
88 }
89 parser.parse(input, flags, handler, context);
90 return handler.getResult().head();
91 }
92
93
94 }
95
96 // Copyright (c) 2011-2014 Saxonica Limited.