2016-02-18 7 views
7

के लिए Gson serialize शून्य, मैं एक विशिष्ट फ़ील्ड या कक्षा के लिए नल को क्रमबद्ध करना चाहता हूं।विशिष्ट वर्ग या फ़ील्ड

जीएसओएन में, विकल्प serializeNulls() पूरे JSON पर लागू होता है।

उदाहरण:

class MainClass { 
    public String id; 
    public String name; 
    public Test test; 
} 

class Test { 
    public String name; 
    public String value;  
} 

MainClass mainClass = new MainClass(); 
mainClass.id = "101" 
// mainClass has no name. 
Test test = new Test(); 
test.name = "testName"; 
test.value = null; 
mainClass.test = test;  

JSON बनाना का उपयोग कर GSON:

GsonBuilder builder = new GsonBuilder().serializeNulls(); 
Gson gson = builder.create(); 
System.out.println(gson.toJson(mainClass)); 

वर्तमान ouput:

{ 
    "id": "101", 
    "name": null, 
    "test": { 
     "name": "testName", 
     "value": null 
    } 
} 

वांछित उत्पादन:

{ 
    "id": "101", 
    "test": { 
     "name": "testName", 
     "value": null 
    } 
} 

वांछित आउटपुट कैसे प्राप्त करें?

पसंदीदा समाधान निम्नलिखित गुण होता है:

  • एक विशिष्ट टिप्पणी के साथ क्षेत्रों के लिए डिफ़ॉल्ट रूप से नहीं serialize Nulls,
  • को क्रमानुसार nulls करो।

    public interface JsonNullable { 
        boolean isJsonNull(); 
    } 
    

    और इसी TypeAdapter (समर्थन करता है केवल लिखने)

    public class JsonNullableAdapter extends TypeAdapter<JsonNullable> { 
    
        final TypeAdapter<JsonElement> elementAdapter = new Gson().getAdapter(JsonElement.class); 
        final TypeAdapter<Object> objectAdapter = new Gson().getAdapter(Object.class); 
    
        @Override 
        public void write(JsonWriter out, JsonNullable value) throws IOException { 
        if (value == null || value.isJsonNull()) { 
         //if the writer was not allowed to write null values 
         //do it only for this field 
         if (!out.getSerializeNulls()) { 
         out.setSerializeNulls(true); 
         out.nullValue(); 
         out.setSerializeNulls(false); 
         } else { 
         out.nullValue(); 
         } 
        } else { 
         JsonElement tree = objectAdapter.toJsonTree(value); 
         elementAdapter.write(out, tree); 
        } 
        } 
    
        @Override 
        public JsonNullable read(JsonReader in) throws IOException { 
        return null; 
        } 
    } 
    

    इस प्रकार के रूप में यह प्रयोग करें:

+0

@DatoMumladze मैंने अपना प्रश्न अपडेट किया – Martin

+0

मुझे यह सुविधा जीसन में नहीं मिली। यहां कुछ दिलचस्प [लिंक] है (https://mindfirejavaexperts.wordpress.com/2014/03/14/how-to-use-gson-library/) या आप जैक्सन को ऑब्जेक्ट को क्रमबद्ध करने के लिए जैक्सन का उपयोग कर सकते हैं और शून्य मानों को बाहर कर सकते हैं इस एनोटेशन '@JsonSerialize (शामिल = JsonSerialize.Inclusion.NON_NULL) का उपयोग करने वाले विशिष्ट फ़ील्ड ' –

+0

संभावित डुप्लिकेट [जीएसओएन में मूल्य के आधार पर सीरियलाइजेशन से कुछ फ़ील्ड को छोड़कर] (http://stackoverflow.com/questions/13120354/excluding-certain -fields-से-क्रमबद्धता आधारित-ऑन-मूल्य-इन-gson) – tima

उत्तर

0

मैं जब वस्तु अशक्त के रूप में श्रृंखलाबद्ध किया जाना चाहिए की जाँच करने के इंटरफेस हो

public class Foo implements JsonNullable { 
    @Override 
    public boolean isJsonNull() { 
    // You decide 
    } 
} 

कक्षा में जहां फू मान होना चाहिए शून्य के रूप में erialized। ध्यान दें कि foo मान स्वयं शून्य नहीं होना चाहिए, अन्यथा कस्टम एडाप्टर एनोटेशन को अनदेखा कर दिया जाएगा।

public class Bar { 
    @JsonAdapter(JsonNullableAdapter.class) 
    public Foo foo = new Foo(); 
} 
संबंधित मुद्दे