JSON.Net – Change $type field to another name?

http://json.codeplex.com/workitem/22429 “I would rather keep $type hard coded and consistent.” Consistent with what I wonder? http://json.codeplex.com/workitem/21989 I would rather not – I think this is too specific to me and I don’t want to go overboard with settings. At some point I will probably implement this – http://json.codeplex.com/workitem/21856 – allowing people to read/write there own … Read more

Can Json.Net handle a List?

A JValue can only contain simple values like strings, ints, booleans, dates and the like. It cannot contain a complex object. I suspect what you really want is this: List<User> list = LoadUsers(); JObject json = new JObject(); json[“users”] = JToken.FromObject(list); The above will convert the list of User objects into a JArray of JObjects … Read more

How to compare two Json objects using C#

I did a bit more digging and was able to find out why the OP’s test code doesn’t run as expected. I was able to fix it by installing and using the FluentAssertions.Json nuget package. One important thing: Be sure to include using FluentAssertions.Json otherwise false positives may occur. Test code is the following: using … Read more

JSON.NET and nHibernate Lazy Loading of Collections

I was facing the same problem so I tried to use @Liedman’s code but the GetSerializableMembers() was never get called for the proxied reference. I found another method to override: public class NHibernateContractResolver : DefaultContractResolver { protected override JsonContract CreateContract(Type objectType) { if (typeof(NHibernate.Proxy.INHibernateProxy).IsAssignableFrom(objectType)) return base.CreateContract(objectType.BaseType); else return base.CreateContract(objectType); } }

Could not determine JSON object type for type “Class”

In order to add an arbitrary non-primitive POCO to a JArray (or JObject), you must explicitly serialize it, using one of the overloads of JToken.FromObject(): _JArray = new JArray(); string[] amounts = repository.Amounts.Split(‘|’); for (int i = 0; i < amounts.Length; i++) { _JArray.Add(JToken.FromObject( new AmountModel { Amounts = amounts[i], })); } return _JArray; (Note … Read more

JsonSerializerSettings and Asp.Net Core

.Net Core 1.0 RTM comes with CamelCase formatting out-of-the-box. This is a behavior change from RC2. However, if you need to modify it, try this snippet: services.AddMvc() .AddJsonOptions(opt => { var resolver = opt.SerializerSettings.ContractResolver; if (resolver != null) { var res = resolver as DefaultContractResolver; res.NamingStrategy = null; // <<!– this removes the camelcasing } … Read more

SignalR : use camel case

Roll your own Conttract resolver like public class SignalRContractResolver : IContractResolver { private readonly Assembly assembly; private readonly IContractResolver camelCaseContractResolver; private readonly IContractResolver defaultContractSerializer; public SignalRContractResolver() { defaultContractSerializer = new DefaultContractResolver(); camelCaseContractResolver = new CamelCasePropertyNamesContractResolver(); assembly = typeof(Connection).Assembly; } public JsonContract ResolveContract(Type type) { if (type.Assembly.Equals(assembly)) { return defaultContractSerializer.ResolveContract(type); } return camelCaseContractResolver.ResolveContract(type); } } Register … Read more

JsonIgnore attributes not working in ASP.NET?

I had incorrectly resolved the JsonIgnore reference. Note that this attribute exists in more than one namespace: System.Text.Json.Serialization Newtonsoft.Json I had resolved this in VS to System.Text.Json.Serialization.JsonIgnore – however I was using the Newtonsoft library for my actual Serialise/Deserialise – and hence the attribute was ignored. Changing the reference to Newtonsoft.Json.JsonIgnore resolved.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)