Suppose my input is following JSON:
{obj: { x: "", y: "test str" }, myStr: "Hi"}
I would like to remove all empty strings and strings with a value of N/A so that the output becomes:
{obj: { y: "test str" }, myStr: "Hi"}
Note the above input is just a sample input file.
I tried to write some code :
var serializerSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
};
But I'm not able to get the desired output.
If you're using a class model to deserialize into, you can use a custom ContractResolver to filter out the unwanted properties on serialization:
class CustomContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var prop = base.CreateProperty(member, memberSerialization);
if (prop.PropertyType == typeof(string) && prop.Readable)
{
prop.ShouldSerialize = obj =>
{
string val = (string)prop.ValueProvider.GetValue(obj);
return !string.IsNullOrEmpty(val) && !val.Equals("N/A", StringComparison.OrdinalIgnoreCase);
};
}
return prop;
}
}
Then use it like this:
var obj = JsonConvert.DeserializeObject<YourClass>(inputJson);
var serializerSettings = new JsonSerializerSettings
{
ContractResolver = new CustomContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy()
}
};
string outputJson = JsonConvert.SerializeObject(obj, serializerSettings);
Working demo here: https://dotnetfiddle.net/zwpj7I
Alternatively, you can remove the unwanted properties without a class model like this:
var jo = JObject.Parse(inputJson);
var unwantedProperties = jo.Descendants()
.OfType<JProperty>()
.Where(p => p.Value.Type == JTokenType.Null ||
(p.Value.Type == JTokenType.String &&
((string)p.Value == string.Empty ||
string.Equals((string)p.Value, "N/A", StringComparison.OrdinalIgnoreCase))))
.ToList();
foreach (var prop in unwantedProperties)
{
prop.Remove();
}
string outputJson = jo.ToString(Formatting.None);
Demo: https://dotnetfiddle.net/QLhDK4
to ignore default empty string using DefaultValueHandling you need to add DefaultValue to x property
[DefaultValue("")]
public string x{ get; set; }
Update:
Here is a link to a working dotnetfiddle
using System;
using Newtonsoft.Json;
using System.ComponentModel;
public class Program
{
public static void Main()
{
//var myClass = new MyClasss();
//var myObj = new Obj();
//myObj.x="";
//myObj.y="test str";
//myClass.myStr = "Hi";
//myClass.obj= myObj;
string json = #"{obj: { x: '', y: 'test str' }, myStr: 'Hi'}";
MyClasss myClass = JsonConvert.DeserializeObject<MyClasss>(json);
var settings = new JsonSerializerSettings();
settings.NullValueHandling = NullValueHandling.Ignore;
settings.DefaultValueHandling = DefaultValueHandling.Ignore;
Console.WriteLine(JsonConvert.SerializeObject(myClass, settings));
}
}
public class Obj {
[DefaultValue("")]
public string x { get; set; }
[DefaultValue("")]
public string y { get; set; }
}
public class MyClasss {
public Obj obj { get; set; }
[DefaultValue("")]
public string myStr { get; set; }
}
Please take a look at ShouldSerialize.
You should declare a method like this in the class containing property "x":
public bool ShouldSerializex()
{
return x != "" && x != "N/A";
}
Related
I am trying to deserialize JSON file and want to assign to object ScanResult. var text showing all the values but scanresult showing null some null values. https://gyazo.com/ff2ce386f845394c458a88d43a1f30d8
please suggest if I am missing something.
//MY jSon File SCAN Test 1-1543045410222.json 's code
{
"at": 1543045410222,
"i": 1000,
"s": {
"Sensor1": ["OFF"],
"Sensor2": ["OFF"],
"DataReady1": ["OFF"],
"DataReady2": ["OFF"],
"CV1": [5.0],
"CV2": [6.0]
}
}
//ViewModel Code is as below:
public void ResendScanResult()
{
var ScanActivities = scanActivityManager.GetAll();
foreach (var item in ScanActivities)
{
var scanName = item.ScanName;
var dir = _dataFilePath + scanName + "\\";
var jsonFileName = string.Format("{0}{1}-{2}.json", dir, scanName, item.ScanDateEpoch);
string fileName = Path.GetFileName(jsonFileName);
// ScanResult scanResult = new ScanResult();
var text = File.ReadAllText(jsonFileName);
//var scanResults = JsonConvert.DeserializeObject<ScanResult>(text);
Common.Model.ScanResult scanResult = JsonConvert.DeserializeObject<Common.Model.ScanResult>(text);
var Mvm = MonitorViewModel.Instance;
// TargetProvider target = Mvm.GetTargetProvider(scanResult);
// Mvm.PublishToServer(target, scanResult);
}
}
and my scanRescult class code is as below :
namespace ABX.Common.Model
{
public class ScanResult
{
public ScanResult()
{
At = DateTimeOffset.Now.ToUnixTimeMilliseconds();
Interval = 1;
}
public string Name { get; set; }
public long At { get; set; }
public long Interval { get; set; }
public JObject Values { get; set; }
public string FileName { get; set; }
public JObject ToJson()
{
JObject json = new JObject
{
{ "at", At },
{ "i", Interval },
{ "s", Values }
};
return json;
}
Either rename your class properties to match your JSON, rename your JSON to match your class properties, or implement a custom JsonConverter, where you can implement arbitrary mapping.
https://dotnetfiddle.net/R96sPn
I am trying to create AddJsonObject such that the name of External.AddParameter obeys whatever the ContractRevolver is set for External.Serialize
In the example below it is camel casing, but as you can see the output is not camel cased. Changing the ContractResolver should effectively determine the formatting of the name parameter. No additional code can be added to External class
This is a class that I cannot modify:
public static class External
{
public static string Serialize(object obj)
{
JsonSerializer ser = new JsonSerializer{
ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
};
using (StringWriter stringWriter = new StringWriter())
{
using (JsonTextWriter jsonTextWriter = new JsonTextWriter((TextWriter)stringWriter))
{
jsonTextWriter.Formatting = Formatting.Indented;
jsonTextWriter.QuoteChar = '"';
ser.Serialize((JsonWriter)jsonTextWriter, obj);
return stringWriter.ToString();
}
}
}
public static void AddParameter(string name, string str)
{
Console.WriteLine(name + " : " + str);
}
}
Just example class:
public class Product { public Product ChildProduct { get; set; } public string Name { get; set; } public DateTime Expiry { get; set; } public string[] Sizes { get; set; } }
Main:
public class Program
{
public void AddJsonObject(object obj)
{
foreach (var property in obj.GetType().GetProperties())
{
var propValue = property.GetValue(obj, null);
External.AddParameter(property.Name, External.Serialize(propValue));
}
}
public void Main()
{
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[]{"small", "big"};
product.ChildProduct = new Product();
AddJsonObject(product);
}
}
Output:
ChildProduct : {
"expiry": "0001-01-01T00:00:00"
}
Name : "Apple"
Expiry : "2008-12-28T00:00:00"
Sizes : [
"small",
"big"
]
Desired Output:
childProduct : {
"expiry": "0001-01-01T00:00:00"
}
name : "Apple"
expiry : "2008-12-28T00:00:00"
sizes : [
"small",
"big"
]
This demo showcases Serialize using JSON.net with CamelCase, but AddJsonObject should work independent of what json serializer they use or what formatting. This example just showcases a non-trivial example.
My initial attempt consisted of wrapping the object into a parent object. External.Serialize() the wrapper object then somehow feed the results into AddParameter such that the name is the output of the serialization -- Couldn't get this to work right.
Change Line where you initialize ContractResolver (ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()) to
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy()
};
Ok I have a solution that works, but Ill hold off anymore clever answers come up
public void AddJsonObject(object obj)
{
var jsonObj = JObject.Parse(External.Serialize(obj));
foreach (var property in jsonObj.Properties())
{
External.AddParameter(property.Name, External.Serialize( property.Value));
}
}
Currently, am adding the properties and values to the object manually like this example and sending to Dapper.SimpleCRUD to fetch data from Dapper Orm. This is the desired output I would like to achieve.
object whereCriteria = null;
whereCriteria = new
{
CountryId = 2,
CountryName = "Anywhere on Earth",
CountryCode = "AOE",
IsActive = true
};
The following class should build the object in the above mentioned format and return the ready-made object.
public static class WhereClauseBuilder
{
public static object BuildWhereClause(object model)
{
object whereObject = null;
var properties = GetProperties(model);
foreach (var property in properties)
{
var value = GetValue(property, model);
//Want to whereObject according to the property and value. Need help in this part!!!
}
return whereObject;
}
private static object GetValue(PropertyInfo property, object model)
{
return property.GetValue(model);
}
private static IEnumerable<PropertyInfo> GetProperties(object model)
{
return model.GetType().GetProperties();
}
}
This function WhereClauseBuilder.BuildWhereClause(object model) should return the object in expected format (mentiond above). Here is the implementation of how I would like to use.
public sealed class CountryModel
{
public int CountryId { get; set; }
public string CountryName { get; set; }
public string CountryCode { get; set; }
public bool IsActive { get; set; }
}
public class WhereClauseClass
{
public WhereClauseClass()
{
var model = new CountryModel()
{
CountryCode = "AOE",
CountryId = 2,
CountryName = "Anywhere on Earth",
IsActive = true
};
//Currently, won't return the correct object because the implementation is missing.
var whereClauseObject = WhereClauseBuilder.BuildWhereClause(model);
}
}
Maybe something like that:
private const string CodeTemplate = #"
namespace XXXX
{
public class Surrogate
{
##code##
}
}";
public static Type CreateSurrogate(IEnumerable<PropertyInfo> properties)
{
var compiler = new CSharpCodeProvider();
var compilerParameters = new CompilerParameters { GenerateInMemory = true };
foreach (var item in AppDomain.CurrentDomain.GetAssemblies().Where(x => !x.IsDynamic))
{
compilerParameters.ReferencedAssemblies.Add(item.Location);
}
var propertiesCode =
string.join("\n\n", from pi in properties
select "public " + pi.PropertyType.Name + " " + pi.Name + " { get; set; }");
var source = CodeTemplate.Replace("##code##", propertiesCode);
var compilerResult = compiler.CompileAssemblyFromSource(compilerParameters, source);
if (compilerResult.Errors.HasErrors)
{
throw new InvalidOperationException(string.Format("Surrogate compilation error: {0}", string.Join("\n", compilerResult.Errors.Cast<CompilerError>())));
}
return compilerResult.CompiledAssembly.GetTypes().First(x => x.Name == "Surrogate");
}
And now use it:
public static object BuildWhereClause(object model)
{
var properties = GetProperties(model);
var surrogateType = CreateSurrogate(properties);
var result = Activator.CreateInstance(surrogateType);
foreach (var property in properties)
{
var value = GetValue(property, model);
var targetProperty = surrogateType.GetProperty(property.Name);
targetProperty.SetValue(result, value, null);
}
return result;
}
I didn't compile that. It's only written here. Maybe there are some errors. :-)
EDIT:
To use ExpandoObject you can try this:
public static object BuildWhereClause(object model)
{
var properties = GetProperties(model);
var result = (IDictionary<string, object>)new ExpandoObject();
foreach (var property in properties)
{
var value = GetValue(property, model);
result.Add(property.Name, value);
}
return result;
}
But I don't know whether this will work for you.
How to generate JSON of Class meta data.
for eg.
C# Classes
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public Description Description { get; set; }
}
public class Description
{
public string Content { get; set; }
public string ShortContent { get; set; }
}
JSON
[
{
"PropertyName" : "Id",
"Type" : "Int",
"IsPrimitive" : true
},
{
"PropertyName" : "Name",
"Type" : "string",
"IsPrimitive" : true
},
{
"PropertyName" : "IsActive",
"Type" : "bool",
"IsPrimitive" : true
},
{
"PropertyName" : "Description",
"Type" : "Description",
"IsPrimitive" : false
"Properties" : {
{
"PropertyName" : "Content",
"Type" : "string",
"IsPrimitive" : true
},
{
"PropertyName" : "ShortContent",
"Type" : "string",
"IsPrimitive" : true
}
}
},
]
If you define a class that will map your Json Model:
public class PropertyDescription
{
public string PropertyName { get; set; }
public string Type { get; set; }
public bool IsPrimitive { get; set; }
public IEnumerable<PropertyDescription> Properties { get; set; }
}
And then just create a function that read the properties of your object recursively:
public static List<PropertyDescription> ReadObject(Type type)
{
var propertyDescriptions = new List<PropertyDescription>();
foreach (var propertyInfo in type.GetProperties())
{
var propertyDescription = new PropertyDescription
{
PropertyName = propertyInfo.Name,
Type = propertyInfo.PropertyType.Name
};
if (!propertyDescription.IsPrimitive
// String is not a primitive type
&& propertyInfo.PropertyType != typeof (string))
{
propertyDescription.IsPrimitive = false;
propertyDescription.Properties = ReadObject(propertyInfo.PropertyType);
}
else
{
propertyDescription.IsPrimitive = true;
}
propertyDescriptions.Add(propertyDescription);
}
return propertyDescriptions;
}
You can use Json.Net to serialize the result of this function :
var result = ReadObject(typeof(Product));
var json = JsonConvert.SerializeObject(result);
EDIT: Linq solution based on #AmitKumarGhosh answer:
public static IEnumerable<object> ReadType(Type type)
{
return type.GetProperties().Select(a => new
{
PropertyName = a.Name,
Type = a.PropertyType.Name,
IsPrimitive = a.PropertyType.IsPrimitive && a.PropertyType != typeof (string),
Properties = (a.PropertyType.IsPrimitive && a.PropertyType != typeof(string)) ? null : ReadType(a.PropertyType)
}).ToList();
}
...
var result = ReadType(typeof(Product));
json = JsonConvert.SerializeObject(result);
One probable solution -
static void Main(string[] args)
{
var o = typeof(Product).GetProperties().Select(a =>
{
if (a.PropertyType != null && (a.PropertyType.IsPrimitive || a.PropertyType == typeof(string)))
{
return MapType(a);
}
else
{
dynamic p = null;
var t = MapType(a);
var props = a.PropertyType.GetProperties();
if (props != null)
{ p = new { t, Properties = props.Select(MapType).ToList() }; }
return new { p.t.PropertyName, p.t.Type, p.t.IsPrimitive, p.Properties };
}
}).ToList();
var jsonString = JsonConvert.SerializeObject(o);
}
static dynamic MapType(PropertyInfo a)
{
return new
{
PropertyName = a.Name,
Type = a.PropertyType.Name,
IsPrimitive = a.PropertyType != null && a.PropertyType.IsPrimitive
};
}
Try this, concept is get all elements from object to dictionary. Field name and value. For each property create additional elements (using Reflection) in dictionary like Type, IsPrimitive etc. You can use recursion for going throw properties and then serialize this dictionary to JSON.
An example here:
Appending to JSON object using JSON.net
An example of this:
var serialize = new Newtonsoft.Json.JsonSerializer();
var dict = GetDic(new Description());
serialize.Serialize(sr, dict);
And GetDcit implementation:
private List<Dictionary<string, string>> GetDic(object obj)
{
var result= new List<Dictionary<string, string>>();
foreach (var r in obj.GetType().GetProperties())
{
result.Add(new Dictionary<string, string>
{
["PropertyName"] = r.Name,
["Type"] = r.PropertyType.Name,
["IsPrimitive"] = r.GetType().IsPrimitive.ToString(),
});
}
return result;
}
I have a very long and complex JSON to send to an external web service.
The JSON has all the properties at the same level:
public class Request
{
[JsonProperty(PropertyName = "prop1a")]
public string Prop1A;
[JsonProperty(PropertyName = "prop2a")]
public string Prop2A;
[JsonProperty(PropertyName = "prop3a")]
public string Prop3A;
[JsonProperty(PropertyName = "prop1b")]
public string Prop1B;
[JsonProperty(PropertyName = "prop2b")]
public string Prop2B;
[JsonProperty(PropertyName = "prop3b")]
public string Prop3B;
// [...]
}
The resulting JSON:
// valid JSON
{ prop1a: "", prop2a: "", prop3a: "", prop1b: "", prop2b: "", prop3b: "" }
In order to work better I have logically separated similar properties into smaller classes:
public class Request
{
public AggregatedPropsA MyAggregatedPropsA;
public AggregatedPropsB MyAggregatedPropsB;
}
public class AggregatedPropsA
{
[JsonProperty(PropertyName = "prop1a")]
public string Prop1A;
[JsonProperty(PropertyName = "prop2a")]
public string Prop2A;
[JsonProperty(PropertyName = "prop3a")]
public string Prop3A;
}
The problem is that the json string is now invalid string because the properties are serialized on different levels:
// invalid JSON
{ MyAggregatedPropsA: { prop1a: "", prop2a: "", prop3a: ""}, MyAggregatedPropsB: { prop1b: "", prop2b: "", prop3b: "" } }
Is it possible to get a JSON like the first, using the second class structure?
var obj = new { x = new { a = 1, b = 2 }, y = new { c = 3, d = 4 } };
Func<JToken, IEnumerable<JProperty>> flatten = null;
flatten = token => token.Concat(token.SelectMany(t => t.Children().SelectMany(y => flatten(y))))
.OfType<JProperty>()
.Where(p => p.Value is JValue || p.Value is JArray);
var dict = flatten(JToken.FromObject(obj))
.ToDictionary(p => p.Name, p => p.Value);
var json = JsonConvert.SerializeObject(dict);