Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2012, Francis Galiegue <fgaliegue@gmail.com>
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the Lesser GNU General Public License as
   * published by the Free Software Foundation, either version 3 of the
   * License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * Lesser GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package com.github.fge.jsonschema.util;
 
 
 import java.util.Map;

Enumeration for the different types of JSON instances which can be encountered.

In addition to what the JSON RFC defines, JSON Schema has an integer type, which is a numeric value without any fraction or exponent part.

 
 
 public enum NodeType
 {
    
Array nodes
 
     ARRAY("array"),
    
Boolean nodes
 
     BOOLEAN("boolean"),
    
Integer nodes
 
     INTEGER("integer"),
    
Number nodes (ie, decimal numbers)
 
     NULL("null"),
    
Object nodes
 
     NUMBER("number"),
    
Null nodes
 
     OBJECT("object"),
    
String nodes
 
     STRING("string");

    
The name for this type, as encountered in a JSON schema
 
     private final String name;

    
Reverse map to find a node type out of this type's name
 
     private static final Map<StringNodeTypeNAME_MAP;

    
 
     private static final Map<JsonTokenNodeTypeTOKEN_MAP
         = new EnumMap<JsonTokenNodeType>(JsonToken.class);
 
     static {
         .put(.);
         .put(.);
 
         final ImmutableMap.Builder<StringNodeTypebuilder
             = ImmutableMap.builder();
 
         for (final NodeType type: NodeType.values())
            builder.put(type.nametype);
         = builder.build();
    }
    NodeType(final String name)
    {
        this. = name;
    }
    @Override
    public String toString()
    {
        return ;
    }

    
Given a type name, return the corresponding node type

Parameters:
name the type name
Returns:
the node type, or null if not found
    public static NodeType fromName(final String name)
    {
        return .get(name);
    }

    
Given a com.fasterxml.jackson.databind.JsonNode as an argument, return its type. The argument MUST NOT BE NULL, and MUST NOT be a com.fasterxml.jackson.databind.node.MissingNode

Parameters:
node the node to determine the type of
Returns:
the type for this node
    public static NodeType getNodeType(final JsonNode node)
    {
        final JsonToken token = node.asToken();
        final NodeType ret = .get(token);
        Preconditions.checkNotNull(ret"unhandled token type " + token);
        return ret;
    }
New to GrepCode? Check out our FAQ X