<commit_before>package com.google.gson;

import junit.framework.TestCase;

import com.google.gson.TestTypes.ClassWithObjects;

public class NullSerializationTests extends TestCase {
  private Gson gson;

  @Override
  protected void setUp() throws Exception {
    super.setUp();    
    gson = new GsonBuilder()
        .serializeNulls()
        .create();
  }
  
  public void testExplicitSerializationOfNulls() {
    ClassWithObjects target = new ClassWithObjects(null);
    String actual = gson.toJson(target);
    String expected = "{\"bag\":null}";
    assertEquals(expected, actual);
  }

}<commit_msg>added a test to test custom serialization using JsonNull.<commit_after>package com.google.gson;

import java.lang.reflect.Type;

import junit.framework.TestCase;

import com.google.gson.TestTypes.BagOfPrimitives;
import com.google.gson.TestTypes.ClassWithObjects;

public class NullSerializationTests extends TestCase {
  private GsonBuilder gsonBuilder;

  @Override
  protected void setUp() throws Exception {
    super.setUp();    
    gsonBuilder = new GsonBuilder().serializeNulls();
  }
  
  public void testExplicitSerializationOfNulls() {
    Gson gson = gsonBuilder.create();
    ClassWithObjects target = new ClassWithObjects(null);
    String actual = gson.toJson(target);
    String expected = "{\"bag\":null}";
    assertEquals(expected, actual);
  }
  
  private static class ClassWithObjectsSerializer implements JsonSerializer<ClassWithObjects> {
    public JsonElement serialize(ClassWithObjects src, Type typeOfSrc, 
        JsonSerializationContext context) {
      JsonObject obj = new JsonObject();
      obj.add("bag", new JsonNull());
      return obj;
    }    
  }
  
  public void testCustomSerializationOfNulls() {
    gsonBuilder.registerSerializer(ClassWithObjects.class, new ClassWithObjectsSerializer());
    Gson gson = gsonBuilder.create();
    ClassWithObjects target = new ClassWithObjects(new BagOfPrimitives());
    String actual = gson.toJson(target);
    String expected = "{\"bag\":null}";
    assertEquals(expected, actual);
  }
}
{'fraction_non_alphanumeric': 0.09956709956709957, 'fraction_numerical': 0.0, 'mean_word_length': 4.11056511056511, 'pattern_counts': {'":': 3, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4737546', 'n_tokens_mistral': 590, 'n_tokens_neox': 589, 'n_words': 140}