Skip to content

Commit

Permalink
✨ Keep IsRequiredSpecified() but use JsonProperty.Required internally
Browse files Browse the repository at this point in the history
Newtonsoft do all the work for us, handling [JsonProperty(Required = Required.Always)] or [JsonRequired] or [DataMember(IsRequired = true)] and filling the JsonProperty.Required field.
  • Loading branch information
ouvreboite committed Aug 1, 2024
1 parent 5ec8ff2 commit 77994ee
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ public static bool TryGetMemberInfo(this JsonProperty jsonProperty, out MemberIn

return (memberInfo != null);
}

public static bool IsRequiredSpecified(this JsonProperty jsonProperty)
{
return jsonProperty.Required != Required.Default;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private List<DataProperty> GetDataPropertiesFor(JsonObjectContract jsonObjectCon
{
if (jsonProperty.Ignored) continue;

var required = jsonProperty.Required != Required.Default
var required = jsonProperty.IsRequiredSpecified()
? jsonProperty.Required
: jsonObjectContract.ItemRequired ?? Required.Default;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public class JsonPropertyAnnotatedType
[JsonProperty(Required = Required.AllowNull)]
public string StringWithRequiredAllowNull { get; set; }

[DataMember(IsRequired = false)] //As the support for DataMember has been implemented later, JsonRequired should take precedence
[DataMember(IsRequired = false)] //As the support for DataMember has been implemented later, JsonProperty.Required should take precedence
[JsonProperty(Required = Required.Always)]
public string StringWithRequiredAlwaysButConflictingDataMember { get; set; }

[DataMember(IsRequired = true)] //As the support for DataMember has been implemented later, JsonRequired should take precedence
[DataMember(IsRequired = true)] //As the support for DataMember has been implemented later, JsonProperty.Required should take precedence
[JsonProperty(Required = Required.Default)]
public string StringWithRequiredDefaultButConflictingDataMember { get; set; }
}
}
}

0 comments on commit 77994ee

Please sign in to comment.