Can not rename to null token Parameter name: token

0

Follow the extension:

    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;
        }

        var newProperty = new JProperty(newName, property.Value);
        property.Replace(newProperty);
    }

Follow the code below:

string json = string.Empty;
try
{
    json = JsonConvert.SerializeObject(createSubscriber);
    JObject jObject = JObject.Parse(json);
    jObject["streetNumber"].Rename("number");
    jObject["ZipCode"].Rename("zipcode");
    json = jObject.ToString(/*Formatting.None*/);
}
catch (System.Exception ex)
{
    throw ex;
}

See template:

            var createSubscriber = new SubscriberRequest
            {
                Code = "cliente03",
                Email = "[email protected]",
                FullName = "Nome Sobrenome",
                Cpf = "22222222222",
                Phone_Area_Code = "11",
                Phone_Number = "934343434",
                BirthDate_Day = "26",
                BirthDate_Month = "04",
                BirthDate_Year = "1980",
                Address = new Address
                {
                    Street = "Rua Nome da Rua",
                    StreetNumber = "100",
                    Complement = "casa",
                    District = "Nome do Bairro",
                    City = "São Paulo",
                    State = "SP",
                    Country = "BRA",
                    ZipCode = "05015010"
                },
                Billing_Info = new Billing_Info
                {
                    Credit_Card = new Credit_Card
                    {
                        Holder_Name = "Nome Completo",
                        Number = "4111111111111111",
                        Expiration_Month = "06",
                        Expiration_Year = "22"
                    }
                }
            };

See the Address.cs class:

public class Address
{
    [JsonProperty("streetNumber", DefaultValueHandling = DefaultValueHandling.Ignore)]
    public string StreetNumber { get; set; }
    [JsonProperty("street", DefaultValueHandling = DefaultValueHandling.Ignore)]
    public string Street { get; set; }
    [JsonProperty("city", DefaultValueHandling = DefaultValueHandling.Ignore)]
    public string City { get; set; }
    [JsonProperty("complement", DefaultValueHandling = DefaultValueHandling.Ignore)]
    public string Complement { get; set; }
    [JsonProperty("district", DefaultValueHandling = DefaultValueHandling.Ignore)]
    public string District { get; set; }
    [JsonProperty("zipCode", DefaultValueHandling = DefaultValueHandling.Ignore)]
    public string ZipCode { get; set; }
    [JsonProperty("state", DefaultValueHandling = DefaultValueHandling.Ignore)]
    public string State { get; set; }
    [JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore)]
    public string Type { get; set; }
    [JsonProperty("country", DefaultValueHandling = DefaultValueHandling.Ignore)]
    public string Country { get; set; }
}

When I try to execute this line jObject["streetNumber"].Rename("number"); , I get this error:

  

Can not rename to null token Parameter name: token

The idea is to: rename streetNumber to number before doing JsonConvert.SerializeObject(json) .

Any solution?!?!

    
asked by anonymous 06.12.2018 / 21:17

1 answer

2

As you yourself check in the Rename method, the value of the last token is null. This is because JSON does not have the streetNumber or ZipCode field.

I believe you want to access the Address field first, it would look like this:

jObject["Address"]["streetNumber"].Rename("number");
jObject["Address"]["zipCode"].Rename("zipcode");
  

See working at dotnetfiddle .

    
07.12.2018 / 00:29