Skip to main content
added 26 characters in body
Source Link
Artur INTECH
  • 7.6k
  • 3
  • 49
  • 41

October 2023 update:

Another solution using built-in System.Text.Json (.NET Core 3.0+) where an object is self-sufficient and doesn't expose all possible fields:

A passing test (using NUnit):

using NUnit.Framework; namespace Intech.UnitTests { public class UserTests { [Test] public void ConvertsItselfToJson() { var userName = "John"; var user = new User(userName); var actual = user.ToJson(); Assert.AreEqual($"{{\"Name\":\"{userName}\"}}", actual); } } } 

An implementation:

using System.Text.Json; using System.Collections.Generic; namespace Intech { public class User { private readonly string name; public User(string name) { this.name = name; } public string ToJson() { var params = new Dictionary<string, string>{{"Name", name}}; return JsonSerializer.Serialize(params); } } } 

Another solution using built-in System.Text.Json (.NET Core 3.0+) where an object is self-sufficient and doesn't expose all possible fields:

A passing test:

using NUnit.Framework; namespace Intech.UnitTests { public class UserTests { [Test] public void ConvertsItselfToJson() { var userName = "John"; var user = new User(userName); var actual = user.ToJson(); Assert.AreEqual($"{{\"Name\":\"{userName}\"}}", actual); } } } 

An implementation:

using System.Text.Json; using System.Collections.Generic; namespace Intech { public class User { private readonly string name; public User(string name) { this.name = name; } public string ToJson() { var params = new Dictionary<string, string>{{"Name", name}}; return JsonSerializer.Serialize(params); } } } 

October 2023 update:

Another solution using built-in System.Text.Json (.NET Core 3.0+) where an object is self-sufficient and doesn't expose all possible fields:

A passing test (using NUnit):

using NUnit.Framework; namespace Intech.UnitTests { public class UserTests { [Test] public void ConvertsItselfToJson() { var userName = "John"; var user = new User(userName); var actual = user.ToJson(); Assert.AreEqual($"{{\"Name\":\"{userName}\"}}", actual); } } } 

An implementation:

using System.Text.Json; using System.Collections.Generic; namespace Intech { public class User { private readonly string name; public User(string name) { this.name = name; } public string ToJson() { var params = new Dictionary<string, string>{{"Name", name}}; return JsonSerializer.Serialize(params); } } } 
added 1 character in body
Source Link
Artur INTECH
  • 7.6k
  • 3
  • 49
  • 41

Another solution using built-in System.Text.Json (.NET Core 3.0+, .NET 5) where an object is self-sufficient and doesn't expose all possible fields:

A passing test:

using NUnit.Framework; namespace Intech.UnitTests { public class UserTests { [Test] public void ConvertsItselfToJson() { var userName = "John"; var user = new User(userName); var actual = user.ToJson(); Assert.AreEqual($"{{\"Name\":\"{userName}\"}}", actual); } } } 

An implementation:

using System.Text.Json; using System.Collections.Generic; namespace Intech { public class User { private readonly string name; public User(string name) { this.name = name; } public string ToJson() { var params = new Dictionary<string, string>{{"Name", name}}; return JsonSerializer.Serialize(params); } } } 

Another solution using System.Text.Json (.NET Core 3.0+, .NET 5) where an object is self-sufficient and doesn't expose all possible fields:

A passing test:

using NUnit.Framework; namespace Intech.UnitTests { public class UserTests { [Test] public void ConvertsItselfToJson() { var userName = "John"; var user = new User(userName); var actual = user.ToJson(); Assert.AreEqual($"{{\"Name\":\"{userName}\"}}", actual); } } } 

An implementation:

using System.Text.Json; using System.Collections.Generic; namespace Intech { public class User { private readonly string name; public User(string name) { this.name = name; } public string ToJson() { var params = new Dictionary<string, string>{{"Name", name}}; return JsonSerializer.Serialize(params); } } } 

Another solution using built-in System.Text.Json (.NET Core 3.0+) where an object is self-sufficient and doesn't expose all possible fields:

A passing test:

using NUnit.Framework; namespace Intech.UnitTests { public class UserTests { [Test] public void ConvertsItselfToJson() { var userName = "John"; var user = new User(userName); var actual = user.ToJson(); Assert.AreEqual($"{{\"Name\":\"{userName}\"}}", actual); } } } 

An implementation:

using System.Text.Json; using System.Collections.Generic; namespace Intech { public class User { private readonly string name; public User(string name) { this.name = name; } public string ToJson() { var params = new Dictionary<string, string>{{"Name", name}}; return JsonSerializer.Serialize(params); } } } 
edited body
Source Link
Artur INTECH
  • 7.6k
  • 3
  • 49
  • 41

Another solution using System.Text.Json (.NET Core 3.0+, .NET 5) where an object is self-sufficient and doesn't expose all possible fields:

A passing test:

using NUnit.Framework; namespace Intech.UnitTests { public class UserTests { [Test] public void ConvertsItselfToJson() { var userName = "John"; var user = new User(userName); var actual = user.ToJson(); Assert.AreEqual($"{{\"Name\":\"{userName}\"}}", actual); } } } 

An implementation:

using System.Text.Json; using System.Collections.Generic; namespace Intech { public class User { private readonly string Name;name; public User(string name) { this.Namename = name; } public string ToJson() { var params = new Dictionary<string, string>{{"Name", Namename}}; return JsonSerializer.Serialize(params); } } } 

Another solution using System.Text.Json (.NET Core 3.0+, .NET 5) where an object is self-sufficient and doesn't expose all possible fields:

A passing test:

using NUnit.Framework; namespace Intech.UnitTests { public class UserTests { [Test] public void ConvertsItselfToJson() { var userName = "John"; var user = new User(userName); var actual = user.ToJson(); Assert.AreEqual($"{{\"Name\":\"{userName}\"}}", actual); } } } 

An implementation:

using System.Text.Json; using System.Collections.Generic; namespace Intech { public class User { private readonly string Name; public User(string name) { this.Name = name; } public string ToJson() { var params = new Dictionary<string, string>{{"Name", Name}}; return JsonSerializer.Serialize(params); } } } 

Another solution using System.Text.Json (.NET Core 3.0+, .NET 5) where an object is self-sufficient and doesn't expose all possible fields:

A passing test:

using NUnit.Framework; namespace Intech.UnitTests { public class UserTests { [Test] public void ConvertsItselfToJson() { var userName = "John"; var user = new User(userName); var actual = user.ToJson(); Assert.AreEqual($"{{\"Name\":\"{userName}\"}}", actual); } } } 

An implementation:

using System.Text.Json; using System.Collections.Generic; namespace Intech { public class User { private readonly string name; public User(string name) { this.name = name; } public string ToJson() { var params = new Dictionary<string, string>{{"Name", name}}; return JsonSerializer.Serialize(params); } } } 
deleted 24 characters in body
Source Link
Artur INTECH
  • 7.6k
  • 3
  • 49
  • 41
Loading
added 50 characters in body
Source Link
Artur INTECH
  • 7.6k
  • 3
  • 49
  • 41
Loading
Source Link
Artur INTECH
  • 7.6k
  • 3
  • 49
  • 41
Loading