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

  • Home
  • SEARCH
  • 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 3407694
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:46:57+00:00 2026-05-18T05:46:57+00:00

When developing I like to have strongly typed objects for certain values. An example

  • 0

When developing I like to have strongly typed objects for certain values. An example could be a username. I’ve managed to create these value objects with validation of business rules in the constructor (the objects are immutable), but I when serializing these objects they behave funky.

[DebuggerDisplay("{Value}")]
public class Username
{
    private readonly string value;

    public Username(string value)
    {
        if (string.IsNullOrEmpty(value))
            throw new ArgumentException("value", "Must have a value");

        if (value.Length > 50)
            throw new ArgumentOutOfRangeException("value", "Maximum length of a username is 50");

        this.value = value;
    }

    public string Value
    {
        get { return value; }
    }

    public static implicit operator string(Username username)
    {
        return username.Value;
    }

    public static explicit operator Username(string value)
    {
        return new Username(value);
    }
}

My question is: Using a serializer such as Newtonsoft Json.Net how go i get this value to be serialized as { Username:"foobar" }?

  • 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-18T05:46:58+00:00Added an answer on May 18, 2026 at 5:46 am

    Never tried this but I’d guess it support type converters.

    Add the TypeConverter attrib to your class and create a converter for it (from string to username and vice versa)

    Newtonsoft.json.net seems to support this:
    http://www.symbolsource.org/Public/Metadata/Project/Json.NET/3.5-Release-6/Release/net-3.5/Newtonsoft.Json/Newtonsoft.Json/Utilities/ConvertUtils.cs

    See the portion of the code starting with
    “// see if source or target types have a TypeConverter that converts …”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a Java desktop application and would like to have an external
If I understand this correctly: Current CPU developing companies like AMD and Intel have
I'm developing a web service (in asp.net) and I would like to have each
Like most developers here and in the entire world, I have been developing software
I would like to begin developing for the Blackberry platform and, specifically, the Bold
I like to keep javascript debugging enabled in my browser so when I'm developing
I'm developing a 3-column website using a layout like this: <div id='left' style='left: 0;
I am developing a Win32 application and I would like to use an RSA
We're developing a windows mobile 6.1 application and would like to make the user
I am developing an application for video capture and I would like to implement

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.