skip to Main Content

I’ve got a fixedPoint math library objects that have the actual values in a few fields and then like a billion properties that seem to hold converted versions of those values to pass along or something?

I.E. 
//important info
public fp x = 0;
public fp y = 0;

//not important info
public fp4 xxxx { get; }
public fp4 xxxy { get; }
public fp4 xxyx { get; }
etc times a billion...

Obviously the only thing I want serialized are the important variables. I spent hours trying to figure out why json was trying to compare variables I didn’t make only to find out that it was because of these properties while it was checking for circular references and throwing me tons of errors. And now I’m ripping my hair out over what seems like should be basic functionality XD. Any help would be greatly appreciated. Bonus points if I don’t have to edit the library files themselves since that might break if I update from that repo.

To reiterate, I only want to serialize fields. No properties.

2

Answers


  1. If you can modify property declaration

    You can use [JsonIgnore] attribute over unwanted props.

    Define a list of ignored props

    Use this helper https://github.com/jitbit/JsonIgnoreProps.
    You can specify list of members to ignore.

    Use it like

    JsonConvert.SerializeObject(
        YourObject,
        new JsonSerializerSettings() {
            ContractResolver = new IgnorePropertiesResolver(new[] { "notImportant1", "notImportant2" })
        };
    );
    

    or use custom contract resolver, filtering out everything but fields

    NOTE – I didn’t test it as have no ability right now, you may need to adjust it

    using Newtonsoft.Json;
    using Newtonsoft.Json.Serialization;
    using System.Reflection;
    
    public class FieldsOnlyContractResolver : DefaultContractResolver
    {
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);
    
            // check member types
            if (property.MemberType == MemberTypes.Property)
            {
                property.ShouldSerialize = _ => false; // Ignore all properties
            }
    
            return property;
        }
    }
    

    Then use it like:

    var settings = new JsonSerializerSettings
    {
        ContractResolver = new FieldsOnlyContractResolver()
    };
    
    string json = JsonConvert.SerializeObject(yourObj, settings);
    
    Login or Signup to reply.
  2. I assume that problem can be in data type which you’re trying to save.

    public fp x = 0;

    In your case fp should be primitive type and serializable. Same restrictions to object which store this info.
    If FixedPoint data can’t be serializalbe then you should convert it to some type which can.
    For ex.:

       [Serializable]
        public struct ImportantFpData //this is replacement for fp x, fp y
        {
           public int x;
           public int y;
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search