Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5990101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:11:44+00:00 2026-05-22T23:11:44+00:00

Suppose I am using Google’s Gson library to parse JSON into Java data structures.

  • 0

Suppose I am using Google’s Gson library to parse JSON into Java data structures.

Is there an easy way to throw an exception if there is a Java field that has no corresponding JSON? That is, I wish to require the JSON to have all the fields in the Java structure.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:11:45+00:00Added an answer on May 22, 2026 at 11:11 pm

    Gson doesn’t have a JSON schema validation feature to specify that a particular element must be present, and it doesn’t have a way to specify that a Java member must be populated. It might be nice to have such a feature available, such as with an @Required annotation. Head on over to the Gson Issues List and put in an enhancement request.

    With Gson, you could enforce that specified JSON elements are present with a custom deserializer.

    // output: 
    //   [MyObject: element1=value1, element2=value2, element3=value3]
    //   [MyObject: element1=value1, element2=value2, element3=null]
    //   Exception in thread "main" com.google.gson.JsonParseException: Required Field Not Found: element2
    
    import java.lang.reflect.Type;
    import java.util.ArrayList;
    import java.util.List;
    
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import com.google.gson.JsonDeserializationContext;
    import com.google.gson.JsonDeserializer;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParseException;
    
    public class Foo
    {
      static String jsonInput1 = "{\"element1\":\"value1\",\"element2\":\"value2\",\"element3\":\"value3\"}";
      static String jsonInput2 = "{\"element1\":\"value1\",\"element2\":\"value2\"}";
      static String jsonInput3 = "{\"element1\":\"value1\",\"element3\":\"value3\"}";
    
      public static void main(String[] args)
      {
        GsonBuilder gsonBuilder = new GsonBuilder();
        MyDeserializer deserializer = new MyDeserializer();
        deserializer.registerRequiredField("element2");
        gsonBuilder.registerTypeAdapter(MyObject.class, deserializer);
        Gson gson = gsonBuilder.create();
        MyObject object1 = gson.fromJson(jsonInput1, MyObject.class);
        System.out.println(object1);
        MyObject object2 = gson.fromJson(jsonInput2, MyObject.class);
        System.out.println(object2);
        MyObject object3 = gson.fromJson(jsonInput3, MyObject.class);
        System.out.println(object3);
      }
    }
    
    class MyObject
    {
      String element1;
      String element2;
      String element3;
    
      @Override
      public String toString()
      {
        return String.format(
            "[MyObject: element1=%s, element2=%s, element3=%s]",
            element1, element2, element3);
      }
    }
    
    class MyDeserializer implements JsonDeserializer<MyObject>
    {
      List<String> requiredFields = new ArrayList<String>();
    
      void registerRequiredField(String fieldName)
      {
        requiredFields.add(fieldName);
      }
    
      @Override
      public MyObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
          throws JsonParseException
      {
        JsonObject jsonObject = (JsonObject) json;
        for (String fieldName : requiredFields)
        {
          if (jsonObject.get(fieldName) == null)
          {
            throw new JsonParseException("Required Field Not Found: " + fieldName);
          }
        }
        return new Gson().fromJson(json, MyObject.class);
      }
    }
    

    A preferable approach might be to use an API that provides JSON Schema validation. Jackson has at least a rudimentary implementation available. JSON Tools looks to have a more mature one.

    Here’s an example with Jackson.

    // output: 
    // Validating jsonInput1...
    // Validating jsonInput2...
    // Validating jsonInput3...
    // $.element2: is missing and it is not optional
    // [MyObject: element1=value1, element2=value2, element3=value3]
    // [MyObject: element1=value1, element2=value2, element3=null]
    // [MyObject: element1=value1, element2=null, element3=value3]
    
    import java.util.List;
    
    import org.codehaus.jackson.map.ObjectMapper;
    
    import eu.vahlas.json.schema.JSONSchema;
    import eu.vahlas.json.schema.JSONSchemaProvider;
    import eu.vahlas.json.schema.impl.JacksonSchemaProvider;
    
    public class Foo
    {
      static String jsonSchema = 
        "{" + 
            "\"description\":\"Serialized MyObject Specification\"," + 
            "\"type\":[\"object\"]," + 
            "\"properties\":" + 
            "{" + 
                "\"element1\":{\"type\":\"string\"}," + 
                "\"element2\":{\"type\":\"string\",\"optional\":false}," + 
                "\"element3\":{\"type\":\"string\",\"optional\":true}" + 
            "}" + 
        "}";;
    
      static String jsonInput1 = "{\"element1\":\"value1\",\"element2\":\"value2\",\"element3\":\"value3\"}";
      static String jsonInput2 = "{\"element1\":\"value1\",\"element2\":\"value2\"}";
      static String jsonInput3 = "{\"element1\":\"value1\",\"element3\":\"value3\"}";
    
      public static void main(String[] args) throws Exception
      {
        ObjectMapper mapper = new ObjectMapper();
        JSONSchemaProvider schemaProvider = new JacksonSchemaProvider(mapper);
        JSONSchema schema = schemaProvider.getSchema(jsonSchema);
    
        System.out.println("Validating jsonInput1...");
        validateAndLogErrors(jsonInput1, schema);
        System.out.println("Validating jsonInput2...");
        validateAndLogErrors(jsonInput2, schema);
        System.out.println("Validating jsonInput3...");
        validateAndLogErrors(jsonInput3, schema);
    
        MyObject object1 = mapper.readValue(jsonInput1, MyObject.class);
        System.out.println(object1);
        MyObject object2 = mapper.readValue(jsonInput2, MyObject.class);
        System.out.println(object2);
        MyObject object3 = mapper.readValue(jsonInput3, MyObject.class);
        System.out.println(object3);
      }
    
      static void validateAndLogErrors(String jsonInput, JSONSchema schema)
      {
        List<String> errors = schema.validate(jsonInput);
        for (String error : errors)
        {
          System.out.println(error);
        }
      }
    }
    
    class MyObject
    {
      String element1;
      String element2;
      String element3;
    
      void setElement1(String element1)
      {
        this.element1 = element1;
      }
    
      void setElement2(String element2)
      {
        this.element2 = element2;
      }
    
      void setElement3(String element3)
      {
        this.element3 = element3;
      }
    
      @Override
      public String toString()
      {
        return String.format(
            "[MyObject: element1=%s, element2=%s, element3=%s]",
            element1, element2, element3);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple question. Is there a way ( using reflections I suppose
When debugging browser-based apps using Google Chrome's Developer Tools, is there a way to
I'm considering building a web app using Java and Google Web Toolkit and I
I'm currently messing with google's apis using ajax trough jquery library. In order to
I am currently working on a small project using RESTlet on Google App Engine/Java.
I'm using Boxy.js to call my /signup page into a modal window. I suppose
I'm using Google Guice for dependency injection. Suppose I have the following: public interface
Let's suppose I'm using the Northwind database and I would like to run a
Suppose I want to open a file in an existing Emacs session using su
Why is using '*' to build a view bad ? Suppose that you have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.