Imports System Imports Newtonsoft.Json Imports Newtonsoft.Json.Converters Imports System.Runtime.CompilerServices Namespace cModalENS ''' ''' CreateEnsDeclaration ''' Partial Public Class ApidogModel Public Property Manifesto As Manifesto End Class Partial Public Class Manifesto Public Property NctsJsonData As NctsJsonData End Class Partial Public Class NctsJsonData ''' ''' Code of the arrival customs ''' Public Property ArrivalCustomCode As String ''' ''' Carrier Address ''' Public Property CarrierAddress As String ''' ''' Carrier City ''' Public Property CarrierCity As String ''' ''' Carrier Country Code ''' Public Property CarrierCountryId As String ''' ''' Carrier Name ''' Public Property CarrierName As String ''' ''' Carrier Postcode ''' Public Property CarrierPostcode As String ''' ''' Carrier Tax No ''' Public Property CarrierTaxno As String ''' ''' Container (if cargo is containerized) ''' Public Property Container As String ''' ''' Code of the departure customs ''' Public Property DepartureCustomCode As String ''' ''' Destination Country Code ''' Public Property DestinationCountryId As String ''' ''' Dispatch Country Code ''' Public Property DispatchCountryId As String ''' ''' Type of the declaration. Should be "ENS". ''' Public Property DocType As DocType ''' ''' Voyage NO ''' Public Property EnsVoyageNo As String Public Property GoodsAttributes As GoodsAttribute() ''' ''' Departure Place ''' Public Property LoadPlace As String ''' ''' RORO Operator ''' Public Property RoroOperatorCode As String ''' ''' Seal NO ''' Public Property SealInfo As String ''' ''' Trailer Code/Plate ''' Public Property TrailerCode As String ''' ''' Trailer Country Code ''' Public Property TrailerCoun As String ''' ''' Transit Method ''' Public Property TransMethod As TransMethod ''' ''' Transit Countries (write it for every transit country) ''' Public Property TransitCountriesAttributes As TransitCountriesAttribute() ''' ''' Destination Place ''' Public Property UnloadPlace As String ''' ''' Vehicle Code/Plate ''' Public Property VehicleCode As String ''' ''' Vehicle Country Code ''' Public Property VehicleCoun As String End Class Partial Public Class GoodsAttribute ''' ''' Gross weight of goods ''' Public Property BrutWg As Double? ''' ''' Goods description ''' Public Property Commodity As String ''' ''' Consignee's address ''' Public Property ConsigneeAddress As String ''' ''' Consignee's city ''' Public Property ConsigneeCity As String ''' ''' Consignee's country code ''' Public Property ConsigneeCountryId As String ''' ''' Consignee's name ''' Public Property ConsigneeName As String ''' ''' Consignee's postcode ''' Public Property ConsigneePostcode As String ''' ''' Consignee's tax number ''' Public Property ConsigneeTaxno As String ''' ''' Container number ''' Public Property ContainerNo As String ''' ''' HS code for the goods ''' Public Property GtipCode As String ''' ''' Invoice amount ''' Public Property InvoiceAmount As Double ''' ''' Net weight of goods ''' Public Property NetWg As Double? ''' ''' Packaging details ''' Public Property PacksAttributes As PacksAttribute() ''' ''' Related documents ''' Public Property ProducedDocumentsAttributes As ProducedDocumentsAttribute() ''' ''' Sender's address ''' Public Property SenderAddress As String ''' ''' Sender's city ''' Public Property SenderCity As String ''' ''' Sender's country code ''' Public Property SenderCountryId As String ''' ''' Sender's name ''' Public Property SenderName As String ''' ''' Sender's postcode ''' Public Property SenderPostcode As String ''' ''' Sender's tax number ''' Public Property SenderTaxno As String End Class Partial Public Class PacksAttribute ''' ''' Packaging notes ''' Public Property Notes As String ''' ''' Number of packages ''' Public Property PackCount As Double ''' ''' Pack Code ''' Public Property PackType As String End Class Partial Public Class ProducedDocumentsAttribute ''' ''' Document code ''' Public Property Code As String ''' ''' Line NO ''' Public Property Quantity As Double? ''' ''' Additonal Information ''' Public Property Reason As String ''' ''' Document reference ''' Public Property Reference As String ''' ''' Document Type ''' Public Property Scope As String End Class Partial Public Class TransitCountriesAttribute ''' ''' Country Code ''' Public Property CountryId As String End Class ''' ''' Type of the declaration. Should be "ENS". ''' Public Enum DocType Ens End Enum ''' ''' Transit Method ''' Public Enum TransMethod Air Rail Road RoroAccompanied RoroUnaccompanied End Enum Partial Public Class ApidogModel Public Shared Function FromJson(json As String) As ApidogModel Return JsonConvert.DeserializeObject(Of ApidogModel)(json, Settings) End Function End Class Public Module Serialize Public Function ToJson(Meself As ApidogModel) As String Return JsonConvert.SerializeObject(Meself, Settings) End Function End Module Friend Module Converter Public ReadOnly Settings As JsonSerializerSettings = New JsonSerializerSettings With { .MetadataPropertyHandling = MetadataPropertyHandling.Ignore, .DateParseHandling = DateParseHandling.None } End Module Friend Class DocTypeConverter Inherits JsonConverter Public Overrides Function CanConvert(t As Type) As Boolean Return t Is GetType(DocType) OrElse t Is GetType(DocType?) End Function Public Overrides Function ReadJson(reader As JsonReader, t As Type, existingValue As Object, serializer As JsonSerializer) As Object If reader.TokenType = JsonToken.Null Then Return Nothing Dim value = serializer.Deserialize(Of String)(reader) If value Is "ENS" Then Return DocType.Ens End If Throw New Exception("Cannot unmarshal type DocType") End Function Public Overrides Sub WriteJson(writer As JsonWriter, untypedValue As Object, serializer As JsonSerializer) If untypedValue Is Nothing Then serializer.Serialize(writer, Nothing) Return End If Dim value = CType(untypedValue, DocType) If value = DocType.Ens Then serializer.Serialize(writer, "ENS") Return End If Throw New Exception("Cannot marshal type DocType") End Sub Public Shared ReadOnly Singleton As DocTypeConverter = New DocTypeConverter() End Class Friend Class TransMethodConverter Inherits JsonConverter Public Overrides Function CanConvert(t As Type) As Boolean Return t Is GetType(TransMethod) OrElse t Is GetType(TransMethod?) End Function Public Overrides Function ReadJson(reader As JsonReader, t As Type, existingValue As Object, serializer As JsonSerializer) As Object If reader.TokenType = JsonToken.Null Then Return Nothing Dim value = serializer.Deserialize(Of String)(reader) Select Case value Case "air" Return TransMethod.Air Case "rail" Return TransMethod.Rail Case "road" Return TransMethod.Road Case "roro_accompanied" Return TransMethod.RoroAccompanied Case "roro_unaccompanied" Return TransMethod.RoroUnaccompanied End Select Throw New Exception("Cannot unmarshal type TransMethod") End Function Public Overrides Sub WriteJson(writer As JsonWriter, untypedValue As Object, serializer As JsonSerializer) If untypedValue Is Nothing Then serializer.Serialize(writer, Nothing) Return End If Dim value = CType(untypedValue, TransMethod) Select Case value Case TransMethod.Air serializer.Serialize(writer, "air") Return Case TransMethod.Rail serializer.Serialize(writer, "rail") Return Case TransMethod.Road serializer.Serialize(writer, "road") Return Case TransMethod.RoroAccompanied serializer.Serialize(writer, "roro_accompanied") Return Case TransMethod.RoroUnaccompanied serializer.Serialize(writer, "roro_unaccompanied") Return End Select Throw New Exception("Cannot marshal type TransMethod") End Sub Public Shared ReadOnly Singleton As TransMethodConverter = New TransMethodConverter() End Class End Namespace