-2

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.

Edit: For anyone else having this problem. I couldn't get Newtonsoft to do what I wanted but the regular built-in JsonUtility worked just fine with no extra steps:

JsonUtility.ToJson(data)

I had tried it earlier but had issues, turns out my dumb self had forgotten a [Serializable] tag on one of my structs Very embarassing.

After that though, I realized that didn't fit some other unrelated needs I had either so I ended up switching to OdinInspectors serializer which ALSO worked fine without any other steps. It even supports serializing System.Object!

Sirenix.Serialization.SerializationUtility.SerializeValue(data, Sirenix.Serialization.DataFormat.JSON);

Clearly what I wanted was basic enough functionality to make the default in other stuff lol.

Thanks to everyone who responded! And for those of you who downvoted, I'm not sure what else you wanted from me ¯\(ツ)

frostymm
  • 19
  • 1
  • 6
  • What is the difference between important and not important. How a compiler should recognize it? – Serge Jul 27 '23 at 00:07
  • Just fields, no properties. To clarify, I don't need to differentiate between fields. I want all the fields. And none of the properties. – frostymm Jul 27 '23 at 01:27
  • see e.g. https://stackoverflow.com/q/10169648/7111561, https://stackoverflow.com/questions/31104335/ignore-base-class-properties-in-json-net-serialization – derHugo Jul 27 '23 at 06:59
  • The DataContract solution seemed pretty good but both [DataMember] and using [JsonProperty] get ignored for some reason when I actually try to serialize. using Unity.Plastic.Newtonsoft.Json.JsonConvert.SerializeObject(data) for reference. – frostymm Jul 27 '23 at 17:06

2 Answers2

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 (member.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);
  • I don't want to do the first two because it requires modifying the library and there are a TON of properties I'd need to add that attribute to. I tried fiddling with a contract resolver before and couldn't quite get it to work. With this one though, I can't seem to find "property.MemberType" it doesn't have that variable. – frostymm Jul 27 '23 at 16:51
  • Updated the answer. Just replace. property.MemberType with member.MemberType. Sorry for typo. – Vlad Sydorenko Jul 27 '23 at 18:02
  • Thanks for updating! Unfortunately it is still trying to serialize my properties though. – frostymm Jul 27 '23 at 20:12
0

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;
    }
CuriousFeo
  • 51
  • 1
  • My problem is that properties are serializing alongside the fields. The fields serialize just fine. – frostymm Jul 27 '23 at 16:36
  • Now I realized your problem, thanks for explanation. I can suggest you two options: 1. How about split your main entity (class or struct) into two sub entites? Something like that: `[Serializable] \n public struct SerializableData
    {
    //fields
    }
    public struct NonSerializableData
    {
    //properties
    }
    public struct AllData
    {
    SerializableData sd;
    NonSerializableData nsd;
    }`
    Or write your own [custom JsonConverter](https://stackoverflow.com/a/24726372/22293992)
    – CuriousFeo Jul 31 '23 at 12:10
  • Sorry for ugly previous comment. Still learning how to do it properly – CuriousFeo Jul 31 '23 at 12:35