4

I am trying to set the order in which various JSON properties get serialised, and all the examples I can find are using C#, and are like:

[JsonProperty(Order = 1)]

but I cannot find a way to write this in VB.NET which Visual Studio will accept - the obvious:

<JsonProperty(Order = 1)>  

gives errors and won't compile .... (no doubt there's a way to format that last line, but...)

As I also need to set the property name for the same property, e.g.

[JsonProperty(PropertyName = "CardCode")]

in c#, how can I set both the name and order in vb.net using JsonPropertyAttribute?

dbc
  • 104,963
  • 20
  • 228
  • 340
PSU
  • 175
  • 1
  • 9
  • Do ` Public Property MyProperty as MyType`. You can see an example at [Deserializing JSON in Visual Basic .NET](https://stackoverflow.com/a/31908231/3744182) and the linked fiddle https://dotnetfiddle.net/9Sw9Sw. If you need more help, can you share your current code? – dbc May 08 '18 at 17:33
  • Thank you - that at least compiles! Whether r not it does the job or notw e shall see in due course... :) As the properties in this class also requrie another jsonproperty (name, to link them to the .NET data classes I (de)serialise to/from, it's interesting that to use both I need to use a line continuation character eg: _ Public Property CardCode As String though putting them on the same line doesn't work! Oh well... – PSU May 08 '18 at 17:44
  • Oh no - spoke too soon! VS shows no syntax errors until trying to compile, then it doesn;t like the continuation character unless it is preceded by a space (fair enough) but then putting one in creates a syntax error with the next line (with the second jsonproperty..... aargh! – PSU May 08 '18 at 17:54
  • 1
    So they go together, k so: – PSU May 08 '18 at 18:02
  • Yes, that's correct. You can only have one `JsonProperty` attribute per property, but it can have many values set. – dbc May 08 '18 at 18:08
  • Yes - thanks again. And not only did it compile, but ti dd the job too :) – PSU May 08 '18 at 18:40

1 Answers1

6

The syntax for applying attributes with parameters in is described in Attributes overview (Visual Basic): Attribute Parameters:

Attribute Parameters

Many attributes have parameters, which can be positional, unnamed, or named. Any positional parameters must be specified in a certain order and cannot be omitted; named parameters are optional and can be specified in any order. Positional parameters are specified first. For example, these three attributes are equivalent:

<DllImport("user32.dll")>  
<DllImport("user32.dll", SetLastError:=False, ExactSpelling:=False)>  
<DllImport("user32.dll", ExactSpelling:=False, SetLastError:=False)>

Thus, if you want to apply JsonPropertyAttribute to a property and set both the name and order, you must do:

Public Class Card
    <JsonProperty(PropertyName:="CardName", Order:=2)>
    Public Property Name As String

    <JsonProperty(PropertyName:="CardDescription", Order:=3, _
            NullValueHandling := NullValueHandling.Ignore, DefaultValueHandling := DefaultValueHandling.IgnoreAndPopulate)>
    <System.ComponentModel.DefaultValue("")>
    Public Property Description As String

    <JsonProperty(PropertyName:="CardCode", Order:=1)>
    Public Property Code As String
End Class

Notes:

  • As shown by the setting AllowMultiple = false in the source code, only one instance of JsonPropertyAttribute can be applied to a given member or parameter:

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Parameter, AllowMultiple = false)]
    public sealed class JsonPropertyAttribute : Attribute
    {
         // Contents of the type omitted
    }
    

    Thus all necessary JsonPropertyAttribute settings must be initialized in that one attribute.

  • The line continuation character _ can be used to break attribute settings across multiple lines. Attributes can be applied on the line(s) immediately preceding a property, however, so it is not necessary to use it in this case.

  • According to the JSON standard a JSON object is an unordered set of name/value pairs, so it is often not necessary to specify the order.

Sample VB.NET fiddle here.

dbc
  • 104,963
  • 20
  • 228
  • 340