0

I have an object with a decimal in:

public class MyObject
{  
   [JsonProperty(PropertyName = "some_amount")]
   public decimal Amount { get; set; }
}

When I call my implementation of MassTransit's IPublishEndpoint.Publish(myObject) the message that actually goes on the queue is similar to this:

{
   ...
   "message":{
      "Amount":"0.1"
   }
   ...
}

Yet when I use JsonConvert.SerializeObject(myObject) I get the following:

{
   "Amount":0.1
}

Ignoring the container object that is included by MassTransit, you can see MyObject's decimal property is in quotations when sent via MassTransit but without them when using Newtonsoft.Json.

I would like MassTransit to serialize my decimal property without the quotations, as a number rather than as a string -- i.e. the same way it get serialized when I serialize the object directly with JsonConvert.SerializeObject. What is the best way to achieve this? I don't want to go re-inventing the wheel if there's a more simplistic route.

Also, is this happening because by default MassTransit serializes decimals to strings? If so, why is this and why haven't Newtonsoft followed for the same reason?

dbc
  • 104,963
  • 20
  • 228
  • 340
Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
  • 1
    I'm not sure what you asking, but you're seeing this because MassTransit uses a [StringDecimalConverter](https://github.com/MassTransit/MassTransit/blob/master/src/MassTransit/Serialization/JsonConverters) in it's [JsonMessageSerializer](https://github.com/MassTransit/MassTransit/blob/master/src/MassTransit/Serialization/JsonMessageSerializer.cs) – stuartd Mar 18 '19 at 14:16
  • I'm asking the easiest way to achieve the same result from MassTransit. If there isn't an easier way than removing the Converter then fair enough, but I thought it was worth finding out. – Lloyd Powell Mar 18 '19 at 15:39
  • Removing the converter is the only way, which can be done via the serializer settings. – Chris Patterson Mar 18 '19 at 15:48
  • 1
    To disable the `StringDecimalConverter` for your property that MassTransit is applying globally, you can apply `[JsonConverter(typeof(NoConverter))]` to the property, where `NoConverter` comes from [this answer](https://stackoverflow.com/a/39739105/3744182) to [Selectively use default JSON converter](https://stackoverflow.com/q/39738714/3744182). Does that answer your question? – dbc Mar 18 '19 at 17:31
  • @dbc that's a good alternative. I just didn't want to remove the converter if there was a tidier way. I would much prefer to do thing's properly than dirtily. Thanks :-) – Lloyd Powell Mar 19 '19 at 09:13
  • Just remember, that the JSON double type isn't as precise as the decimal type in .NET. – Chris Patterson Mar 19 '19 at 15:26

0 Answers0