Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Text.Json support a way to ignore/handle exceptions on serialization #44390

Closed
gokhanabatay opened this issue Nov 8, 2020 · 6 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json
Milestone

Comments

@gokhanabatay
Copy link

NHibernate Reference properties may throw record not found exception on serialization.
With serialization options we could ignore this exception, please give us a way to ignore errors on serialization.

    public static class ObjectExtension
    {
        public static T DeepCopy<T>(this T value, bool throwsException = false)
        {
            if (value == null)
            {
                return value;
            }

            string json = JsonConvert.SerializeObject(value, new JsonSerializerSettings()
            {
                ContractResolver = new NHibernateContractResolver(),
                Error = (se, ev) => { ev.ErrorContext.Handled = !throwsException; }
            });

            return JsonConvert.DeserializeObject<T>(json);
        }
    }

NHibernate Proxy objects serialization how to define global or property level contract resolver?

 public class NHibernateContractResolver : CamelCasePropertyNamesContractResolver
    {
        protected override JsonContract CreateContract(Type objectType)
        {
            if (typeof(NHibernate.Proxy.INHibernateProxy).IsAssignableFrom(objectType))
            {
                return base.CreateContract(objectType.BaseType);
            }
            else
            {
                return base.CreateContract(objectType);
            }
        }
    }
@gokhanabatay gokhanabatay added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Nov 8, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Nov 8, 2020
@gokhanabatay
Copy link
Author

gokhanabatay commented Nov 8, 2020

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
Hi I don't have write-permissions
area-System.Text.Json

@gokhanabatay gokhanabatay changed the title Support a way to ignore/handle exceptions on serialization System.Text.Json support a way to ignore/handle exceptions on serialization Nov 8, 2020
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Nov 12, 2020
@layomia layomia added this to the Future milestone Nov 12, 2020
@layomia
Copy link
Contributor

layomia commented Nov 12, 2020

It seems like you want a way to try/catch exceptions when the serializer calls property/field getters on serialization:

  • Should this error ignoring mechanism also be used when calling setters and object constructors? Is a way to specify which member types to include desired?
  • Should the option be global (across the entire object graph), per type, or per property/field/ctor?
  • Is a mechanism to produce all the exceptions encountered per (de)serialization desired?

@gokhanabatay
Copy link
Author

Hi @layomia Newtonsoft impl. gives us an option: if an exception occurs during serialization/deserialization "field/property" "ctor"(not sure but probably) ignore exception and continue serialization/deserialization on next member.
Yes it should be global if we set "ev.ErrorContext.Handled = true" per type/property/field in that serialization.
It's a Serialization/Deserialization option that you can on/off per serialization call as you desired.

@omidkrad
Copy link

At least need a serializer option to ignore property on exception, or set it to undefined. Sometimes can get a property only when a dependent property has a certain value, otherwise it throws exception. And the object comes from a library that I cannot modify so need a way to handle it. Newtonsoft handles this gracefully.

@eiriktsarpalis
Copy link
Member

Duplicate of #38049

@eiriktsarpalis eiriktsarpalis marked this as a duplicate of #38049 Oct 22, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Nov 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json
Projects
None yet
Development

No branches or pull requests

6 participants