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.io.File;
 import java.io.Reader;
 import java.net.URL;

Utility class to load JSON documents (schemas or instance) from various sources as com.fasterxml.jackson.databind.JsonNodes.

This class uses com.github.fge.jsonschema.util.jackson.JacksonUtils.getMapper() as an com.fasterxml.jackson.databind.ObjectMapper to parse JSON inputs.

 
 public final class JsonLoader
 {
    
The mapper which does everything behind the scenes...
 
     private static final ObjectMapper MAPPER = JacksonUtils.getMapper();

    
A shortcut: myself as a java.lang.Class object.
 
     private static final Class<JsonLoadermyself = JsonLoader.class;
 
     private JsonLoader()
     {
     }

    
Read a com.fasterxml.jackson.databind.JsonNode from a resource path. Explicitly throws an java.io.IOException if the resource is null, instead of letting a java.lang.NullPointerException slip through...

Parameters:
resource The path to the resource
Returns:
the JSON document at the resource
Throws:
java.io.IOException if the resource does not exist or there was a problem loading it, or if the JSON document is invalid
 
     public static JsonNode fromResource(final String resource)
         throws IOException
     {
         final InputStream in = .getResourceAsStream(resource);
 
         if (in == null)
             throw new IOException("resource " + resource + " not found");
 
         final JsonNode ret;
 
         try {
             ret = .readTree(in);
         } finally {
             in.close();
         }
 
         return ret;
     }

    
Read a com.fasterxml.jackson.databind.JsonNode from an URL.

Parameters:
url The URL to fetch the JSON document from
Returns:
The document at that URL
Throws:
java.io.IOException in case of network problems etc.
 
     public static JsonNode fromURL(final URL url)
         throws IOException
     {
         return .readTree(url);
     }

    
Read a com.fasterxml.jackson.databind.JsonNode from a file on the local filesystem.

Parameters:
path the path (relative or absolute) to the file
Returns:
the document in the file
Throws:
java.io.IOException if this is not a file, if it cannot be read, etc.
    public static JsonNode fromPath(final String path)
        throws IOException
    {
        final JsonNode ret;
        final FileInputStream in = new FileInputStream(path);
        try {
            ret = .readTree(in);
        } finally {
            in.close();
        }
        return ret;
    }

    
Same as fromPath(java.lang.String), but this time the user supplies the java.io.File object instead

Parameters:
file the File object
Returns:
The document
Throws:
java.io.IOException in many cases!
    public static JsonNode fromFile(final File file)
        throws IOException
    {
        final JsonNode ret;
        final FileInputStream in = new FileInputStream(file);
        try {
            ret = .readTree(in);
        } finally {
            in.close();
        }
        return ret;
    }

    
Read a com.fasterxml.jackson.databind.JsonNode from a user supplied java.io.Reader

Parameters:
reader The reader
Returns:
the document
Throws:
java.io.IOException if the reader has problems
    public static JsonNode fromReader(final Reader reader)
        throws IOException
    {
        return .readTree(reader);
    }

    
Read a com.fasterxml.jackson.databind.JsonNode from a string input

Parameters:
json the JSON as a string
Returns:
the document
Throws:
java.io.IOException could not read from string
    public static JsonNode fromString(final String json)
        throws IOException
    {
        return fromReader(new StringReader(json));
    }
New to GrepCode? Check out our FAQ X