servicestackservicestack-text

Servicestack JsConfig with emitLowercaseUnderscoreNames = true does not work on properties with alphanumeric names


I am using Servicestack JsonConfig for serializing and deserializing the JSON. but for the following class, it works for some properties and does not for others.

        public class Address
         {
            public string Street1 { get; set; }
            public string Street2 { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string ZipCode { get; set; }
         }

          

When I deserialize it to JSON ZipCode is correctly represented as "zip_code" but Street1 is represented as "street1" where the expected presentation is "street_1". Following is the code

    using (JsConfig.With(emitLowercaseUnderscoreNames: true, propertyConvention: PropertyConvention.Lenient))
      {
           //serialize into json
           requestJsonString = JsonSerializer.SerializeToString(request.SubscriptionRequest);
      }

The part of the JSON I am getting is

{"address":{"street1":"100 Tlllbow Street","street2":"100 Taljjow Street","city":"Housthgon","state":"hg","zip_code":"022"}}

Please help. Thanks in Advance.


Solution

  • This behavior is by design, you can change it by providing an alias with [DataMember(Name] attribute, e.g:

    [DataContract]
    public class Address
    {
       [DataMember(Name="street_1")]
       public string Street1 { get; set; }
       [DataMember(Name="street_2")]
       public string Street2 { get; set; }
       [DataMember]
       public string City { get; set; }
       [DataMember]
       public string State { get; set; }
       [DataMember]
       public string ZipCode { get; set; }
    }
    

    Or by renaming your properties:

    public class Address
    {
       public string Street_1 { get; set; }
       public string Street_2 { get; set; }
    }