Advertisement
LocutusOfBorg

JSON

Jul 23rd, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1.  
  2. public class main {
  3.     // First we create a parser to read the validator specification which is
  4.     // defined using the (what did you think) JSON format.
  5.     // The validator definition is located in the "my-validator.json" resource in the
  6.     // class path.
  7.     JSONParser lParser =
  8.             new JSONParser (
  9.                     main.class.getResourceAsStream("my-validator.json"));
  10.  
  11.     // We parse the validator spec and convert it into a Java representation.
  12.     JSONObject lValidatorObject = (JSONObject) lParser.nextValue();
  13.     // Finally we can convert our validator using the Java model.
  14.  
  15.     Validator lValidator = new JSONValidator(lValidatorObject);
  16.  
  17.     // And now that you have the validator, you can start validating your data.
  18.  
  19.     // First we create a parser to read the data.
  20.     JSONParser lParser = new JSONParser(main.class.getResourceAsStream("data.json"));
  21.  
  22.     // We parse the datafile and convert it into a Java representation.
  23.     JSONValue lMyData = lParser.nextValue();
  24.  
  25.     // Now we can use the validator to check on our data. We can test if the data has the
  26.     // correct format or not.
  27.     lValidator.validate(lMyData);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement