Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for both camel case and non camel case parameters #31

Closed
camhart opened this issue Mar 28, 2018 · 7 comments · Fixed by #39
Closed

Support for both camel case and non camel case parameters #31

camhart opened this issue Mar 28, 2018 · 7 comments · Fixed by #39

Comments

@camhart
Copy link

camhart commented Mar 28, 2018

Here's my current setup:

[JsonConverter(typeof(JsonSubtypes), "Type")]
  [JsonSubtypes.KnownSubType(typeof(A), PriceDiscountType.A)]
  [JsonSubtypes.KnownSubType(typeof(B), PriceDiscountType.B)]
  [JsonSubtypes.KnownSubType(typeof(C), PriceDiscountType.C)]
  [JsonSubtypes.KnownSubType(typeof(D), PriceDiscountType.D)]
  [JsonSubtypes.KnownSubType(typeof(E), PriceDiscountType.E)]
  [JsonSubtypes.KnownSubType(typeof(F), PriceDiscountType.F)]
  [JsonSubtypes.KnownSubType(typeof(G), PriceDiscountType.G)]
  public abstract class PriceDiscount : Core.Toolkit.ICloneable

However, at times the default serialization is to use camel case. Is there a way to make this work for both camel case, and non camel case, so that I don't have to worry about which is being used?

@manuc66
Copy link
Owner

manuc66 commented Apr 2, 2018

Hi,

You can tweak the way PriceDiscountType is serialized with Newtonsoft.Json, JsonSubtypes match serialization of PriceDiscountType.X with the value found in the incoming JSON.

@Hauptsatana
Copy link

@manuc66 Thank you for such an awesome library!
Could you please give more details and maybe example for your previous comment?

I had similar issue with case of property names. Here is my current working setup:

[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(FleetInput), "fleet")]
[JsonSubtypes.KnownSubType(typeof(CampInput), "camp")]
public abstract class AbstractOrganizationUnitInput

As @camhart I have "Type" property in C# class, and firstly used [JsonConverter(typeof(JsonSubtypes), "Type")] but exception was thrown during deserialization as my JsonSerializer uses camelCase.
When I changed name to "type", everything got normal.

Could you please explicilty state in documentation that we should use JSON object property name, not the class one? Is it possible to make you converter be aware of JsonSerializer settings to use C# class property name?

@manuc66
Copy link
Owner

manuc66 commented Apr 11, 2018

Hi @Hauptsatana, @camhart

I had read a bit too rapidly the original question (sorry), I thought it was about about the case of the enum. So here is what I thought about... :

[JsonConverter(typeof(StringEnumConverter), true)]
public enum PriceDiscountType
{
    A,
    B
}

--
I confirm that the discriminator property name is the one from the JSON object so that this sample test succeed:

[JsonConverter(typeof(JsonSubtypes), "kind")]
public interface IAnimal
{
	[JsonProperty("kind")]
	string Type { get; }
}

public class Dog : IAnimal
{
	public string Type { get; } = "Dog";
	public string Breed { get; set; }
}

[Test]
public void Demo()
{
	var animal =
		JsonConvert.DeserializeObject<IAnimal>(
			"{\"kind\":\"Dog\",\"Breed\":\"Jack Russell Terrier\"}");
	Assert.AreEqual("Jack Russell Terrier", (animal as Dog)?.Breed);
	Assert.AreEqual("Dog", animal.Type);
}

As for now there is no know workaround to match typeMappingPropertyName with a JSON property name that does not have the same case.

For the documentation concern, as a consumer of this library, where in the documentation would you have expected to find this ? (And feel free to submit a PR ;-))

@manuc66
Copy link
Owner

manuc66 commented Apr 17, 2018

Support for different case is released with v1.4.0 and documentation has been updated

@camhart
Copy link
Author

camhart commented Apr 17, 2018

Is this enabled by default? Or is there some magic way to turn it on?

Thanks btw!

Edit: Looks like I just need to explicitly set the JsonProperty value. Is that correct?

@manuc66
Copy link
Owner

manuc66 commented Apr 18, 2018

@camhart It's enabled by default and

  • should have the same property matching behavior than JSON.NET
  • shouldn't need JsonProperty attribute.

@jbetances

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants