1

I have an object I would like to serialize into json in Unity to send to a service via REST call. In .NET I know you can easily ignore null properties.

[JsonProperty("some_model", NullValueHandling = NullValueHandling.Ignore)]
public class SomeModel
{
    ....
}

Is this possible using FullSerializer in Unity?

Currently I have

fsData data = null;
fsResult r = sm_Serializer.TrySerialize(objectToSerialize, out data);
string sendjson = data.ToString();

Is there a similar attribute I can add to the DataModel using FullSerializer?

[fsObject(ignoreNullProperties)]
public class SomeModel
{
    ....
}

1 Answer 1

1

Looks like one answer is custom converters.

private static fsSerializer sm_Serializer = new fsSerializer();

[fsObject(Converter = typeof(CustomConverter))]
public class SomeClass
{
    string MyProp { get; set; }
}

public class CustomConverter : fsConverter
{
    private static fsSerializer sm_Serializer = new fsSerializer();

    public override bool CanProcess(Type type)
    {
        return type == typeof(SomeClass);
    }

    public override fsResult TryDeserialize(fsData data, ref object instance, Type storageType)
    {
        throw new NotImplementedException();
    }

    public override fsResult TrySerialize(object instance, out fsData serialized, Type storageType)
    {
        SomeClass someClass = (SomeClass)instance;
        serialized = null;

        Dictionary<string, fsData> serialization = new Dictionary<string, fsData>();

        fsData tempData = null;

        if (someClass.MyProp != null)
        {
            sm_Serializer.TrySerialize(someClass.MyProp, out tempData);
            serialization.Add("myProp", tempData);
        }

        serialized = new fsData(serialization);

        return fsResult.Success;
    }
}

This works but any other suggestions are greatly appreciated!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.