Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6700687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:50:40+00:00 2026-05-26T06:50:40+00:00

I have a C# class with an object valued property. I am setting this

  • 0

I have a C# class with an object valued property. I am setting this property to an enum value , serialising to Json and then deserialising back to the object.

How can I make the object’s property value deserialise back to the enum?

That is, given:

public class Foo
{
   public object Value { get; set; }
}
public enum SmallNumbers { One, Two, Three }

How can I make this test pass?

   [Test]
   public void an_object_property_set_to_an_enum_can_be_serialised()
   {
      var settings = new JsonSerializerSettings
                        {
                           TypeNameHandling = TypeNameHandling.Auto
                        };

      var json = JsonConvert.SerializeObject(
         new Foo {Value = SmallNumbers.One},
         Formatting.None,
         settings);

      var foo = JsonConvert.DeserializeObject<Foo>(json, settings);

      Assert.That(foo.Value is SmallNumbers);
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:50:41+00:00Added an answer on May 26, 2026 at 6:50 am

    It’s possible to write a converter for this special case but I won’t be helpful if you have many properties like ‘Value’ of type Object because there’s nothing to tell to which type to convert each Object. Check the code below & run the test on your machine.

    using System;
    using Newtonsoft.Json;
    using NUnit.Framework;
    
    class StackOverflowIssue7801000
    {
        public enum SmallNumbers { One, Two, Three }
    
        public class Foo
        {
            public object Value { get; set; }
        }
    
        class ObjectToSmallNumbersConverter : JsonConverter
        {
            public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
            {
                // Not required for deserialization
                throw new NotImplementedException();
            }
    
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                return (SmallNumbers)Convert.ToInt32(reader.Value);
            }
    
            public override bool CanConvert(Type objectType)
            {
                return (objectType == typeof(object));
            }
        }
    
        [Test]
        public void an_object_property_set_to_an_enum_can_be_serialised()
        {
            var settings = new JsonSerializerSettings {
                TypeNameHandling = TypeNameHandling.All
            };
    
            var json = JsonConvert.SerializeObject(new Foo { Value = SmallNumbers.Three }, Formatting.None, settings);
            settings.Converters.Add(new ObjectToSmallNumbersConverter());
            var foo = JsonConvert.DeserializeObject<Foo>(json, settings);
            Assert.That(foo.Value is SmallNumbers);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with a Property called 'Value' which is of type Object.
We have the Class object (an object that reference a Class) so you can
I've got the class object for an enum (I have a Class<? extends Enum>
I have this class: class View(object): def main_page(self, extra_placeholders = None): file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl'
I have a class: class MyClass(object): @property def myproperty(self): return 'hello' Using mox and
Let's suppose I have this object: [Serializable] public class MyClass { public int Age
I have an Entity. And sometimes I need this object also contains some value,
I have something like this in my Map public class Settings : ClassMap<Setting> {
I have a class that represents some business object, such as this: Public Class
I have a property on my World class that looks like this: public Vec2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.