I've the following property
{
"bad":
{
"Login": "someLogin",
"Street": "someStreet",
"House": "1",
"Flat": "0",
"LastIndication":
[
"230",
"236"
],
"CurrentIndication":
[
"263",
"273"
],
"Photo":
[
null,
null
]
}
}
and how can I rename this from 'bad' to 'good' for example. Yes, I saw the extension method by Abi Bellamkonda
public static class NewtonsoftExtensions
{
public static void Rename(this JToken token, string newName)
{
var parent = token.Parent;
if (parent == null)
throw new InvalidOperationException("The parent is missing.");
var newToken = new JProperty(newName, token);
parent.Replace(newToken);
}
}
but it got this exeption
Can not add Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JProperty.
Somewhat counterintuitively, that extension method assumes that the token
you are passing to it is the value of a JProperty
, not the JProperty
itself. Presumably, this is to make it easy to use with the square bracket syntax:
JObject jo = JObject.Parse(json);
jo["bad"].Rename("good");
If you have a reference to the property, you can still use that extension method if you call it on the property's Value
like this:
JObject jo = JObject.Parse(json);
JProperty prop = jo.Property("bad");
prop.Value.Rename("good");
However, that makes the code seem confusing. It would be better to improve the the extension method so that it will work in both situations:
public static void Rename(this JToken token, string newName)
{
if (token == null)
throw new ArgumentNullException("token", "Cannot rename a null token");
JProperty property;
if (token.Type == JTokenType.Property)
{
if (token.Parent == null)
throw new InvalidOperationException("Cannot rename a property with no parent");
property = (JProperty)token;
}
else
{
if (token.Parent == null || token.Parent.Type != JTokenType.Property)
throw new InvalidOperationException("This token's parent is not a JProperty; cannot rename");
property = (JProperty)token.Parent;
}
// Note: to avoid triggering a clone of the existing property's value,
// we need to save a reference to it and then null out property.Value
// before adding the value to the new JProperty.
// Thanks to @dbc for the suggestion.
var existingValue = property.Value;
property.Value = null;
var newProperty = new JProperty(newName, existingValue);
property.Replace(newProperty);
}
Then you can do:
JObject jo = JObject.Parse(json);
jo.Property("bad").Rename("good"); // works with property reference
jo["good"].Rename("better"); // also works with square bracket syntax
Fiddle: https://dotnetfiddle.net/RSIdfx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With