mirror of
https://github.com/zoriya/octokit.net.git
synced 2025-12-21 06:35:11 +00:00
omit null values from serialized objects
This commit is contained in:
@@ -1,4 +1,10 @@
|
|||||||
namespace Octokit.Http
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Reflection;
|
||||||
|
using Octokit.Reflection;
|
||||||
|
|
||||||
|
namespace Octokit.Http
|
||||||
{
|
{
|
||||||
public class SimpleJsonSerializer : IJsonSerializer
|
public class SimpleJsonSerializer : IJsonSerializer
|
||||||
{
|
{
|
||||||
@@ -20,6 +26,32 @@
|
|||||||
{
|
{
|
||||||
return clrPropertyName.ToRubyCase();
|
return clrPropertyName.ToRubyCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is overridden so that null values are omitted from serialized objects.
|
||||||
|
[SuppressMessage("Microsoft.Design", "CA1007:UseGenericsWhereAppropriate", Justification = "Need to support .NET 2")]
|
||||||
|
protected override bool TrySerializeUnknownTypes(object input, out object output)
|
||||||
|
{
|
||||||
|
if (input == null) throw new ArgumentNullException("input");
|
||||||
|
output = null;
|
||||||
|
Type type = input.GetType();
|
||||||
|
if (type.FullName == null)
|
||||||
|
return false;
|
||||||
|
IDictionary<string, object> obj = new JsonObject();
|
||||||
|
IDictionary<string, ReflectionUtils.GetDelegate> getters = GetCache[type];
|
||||||
|
foreach (KeyValuePair<string, ReflectionUtils.GetDelegate> getter in getters)
|
||||||
|
{
|
||||||
|
if (getter.Value != null)
|
||||||
|
{
|
||||||
|
var value = getter.Value(input);
|
||||||
|
if (value == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
obj.Add(MapClrMemberNameToJsonFieldName(getter.Key), value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output = obj;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user