1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.databind.io.json;
7   
8   import gov.nist.secauto.metaschema.core.model.IBoundObject;
9   import gov.nist.secauto.metaschema.core.model.util.JsonUtil;
10  import gov.nist.secauto.metaschema.databind.io.AbstractProblemHandler;
11  import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelComplex;
12  
13  import java.io.IOException;
14  import java.util.HashSet;
15  import java.util.Set;
16  
17  /**
18   * This problem handler implementation handles common issues when parsing
19   * JSON-based Metaschema module instances.
20   */
21  public class DefaultJsonProblemHandler
22      extends AbstractProblemHandler
23      implements IJsonProblemHandler {
24    private static final String JSON_SCHEMA_FIELD_NAME = "$schema";
25    private static final Set<String> IGNORED_FIELD_NAMES;
26  
27    static {
28      IGNORED_FIELD_NAMES = new HashSet<>();
29      IGNORED_FIELD_NAMES.add(JSON_SCHEMA_FIELD_NAME);
30    }
31  
32    @Override
33    public boolean handleUnknownProperty(
34        IBoundDefinitionModelComplex classBinding,
35        IBoundObject targetObject,
36        String fieldName,
37        IJsonParsingContext parsingContext) throws IOException {
38      boolean retval = false;
39      if (IGNORED_FIELD_NAMES.contains(fieldName)) {
40        JsonUtil.skipNextValue(parsingContext.getReader(), parsingContext.getSource());
41        retval = true;
42      }
43      return retval;
44    }
45  }