Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libraries/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ public sealed partial class JsonSerializerOptions
{
public JsonSerializerOptions() { }
public bool AllowTrailingCommas { get { throw null; } set { } }
public bool AllowPrivateProperties { get { throw null; } set { } }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is an API change, it would need to go through API review. Also, I don't think we want to support non-public properties within the JsonSerializer. I haven't seen evidence that a lot of folks want this feature.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An issue with supporting non-public is that it won't work with code-gen \ AOT scenarios where we can't use reflection.

public System.Collections.Generic.IList<System.Text.Json.Serialization.JsonConverter> Converters { get { throw null; } }
public int DefaultBufferSize { get { throw null; } set { } }
public System.Text.Json.JsonNamingPolicy? DictionaryKeyPolicy { get { throw null; } set { } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ public JsonClassInfo(Type type, JsonSerializerOptions options)
{
CreateObject = options.MemberAccessorStrategy.CreateConstructor(type);

PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the only fix here is to remove the BindingFlags.NonPublic flag. It's not conveying the intent of the serializer and likely just an oversight (maybe copy/paste error). In reality, non-public properties are not supported.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe NonPublic is necessary to support public getters and non-public setters.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Responded in the PR that's removing it: #2278 (comment)

BindingFlags bindingFlags = options.AllowPrivateProperties
? BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
: BindingFlags.Instance | BindingFlags.Public;
PropertyInfo[] properties = type.GetProperties(bindingFlags);

Dictionary<string, JsonPropertyInfo> cache = CreatePropertyCache(properties.Length);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public sealed partial class JsonSerializerOptions
private int _defaultBufferSize = BufferSizeDefault;
private int _maxDepth;
private bool _allowTrailingCommas;
private bool _allowPrivateProperties;
private bool _haveTypesBeenCreated;
private bool _ignoreNullValues;
private bool _ignoreReadOnlyProperties;
Expand Down Expand Up @@ -66,6 +67,26 @@ public bool AllowTrailingCommas
}
}

/// <summary>
/// Determines whether non-public properties should be serialized and deserialized.
/// The default value is false.
/// </summary>
/// <exception cref="InvalidOperationException">
/// Thrown if this property is set after serialization or deserialization has occurred.
/// </exception>
public bool AllowPrivateProperties
{
get
{
return _allowPrivateProperties;
}
set
{
VerifyMutable();
_allowPrivateProperties = value;
}
}

/// <summary>
/// The default buffer size in bytes used when creating temporary buffers.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ namespace System.Text.Json.Serialization.Tests
{
public static class PropertyVisibilityTests
{
[Fact]
public static void Serialize_NonPublicProperty_RequiresOptIn()
{
var obj = new ClassWithPrivateProperty { PrivateProperty = true };
var allowNonPublic = new JsonSerializerOptions { AllowPrivateProperties = true };

string jsonDefault = JsonSerializer.Serialize(obj);
string jsonNonPublic = JsonSerializer.Serialize(obj, allowNonPublic);

Assert.Equal(@"{}", jsonDefault);
Assert.Equal(@"{""PrivateProperty"":true}", jsonNonPublic);

var objDefault = JsonSerializer.Deserialize<ClassWithPrivateProperty>(jsonDefault);
var objNonPublic = JsonSerializer.Deserialize<ClassWithPrivateProperty>(jsonNonPublic, allowNonPublic);

Assert.False(objDefault.PrivateProperty);
Assert.True(objNonPublic.PrivateProperty);
}

public class ClassWithPrivateProperty
{
internal bool PrivateProperty { get; set; }
}

[Fact]
public static void NoSetter()
{
Expand Down