// TradeSync: sample common server module for 1C:Enterprise. // Adapter boundary only. It does not post accounting documents. // This module has not been executed in a licensed 1C runtime. // Items: Array of Structure(sku, name, priceMinor, stock). Function SerializeCatalog(Items, Revision) Export If TypeOf(Items) <> Type("Array") Then Raise "Items must be an Array"; EndIf; If TypeOf(Revision) <> Type("Number") Then Raise "Revision must be a number"; EndIf; If Revision < 1 Or Revision <> Int(Revision) Then Raise "Revision must be a positive integer"; EndIf; Payload = New Structure; Payload.Insert("revision", Revision); Payload.Insert("items", Items); Writer = New JSONWriter; Writer.SetString(); WriteJSON(Writer, Payload); Return Writer.Close(); EndFunction // Parses an order envelope. Business validation and a transaction are required // before writing to a real information base. See README for the protocol. Function ParseOrderEnvelope(JSONText) Export Reader = New JSONReader; Reader.SetString(JSONText); Try Payload = ReadJSON(Reader, False); Except Reader.Close(); Raise; EndTry; Reader.Close(); If TypeOf(Payload) <> Type("Structure") Then Raise "Order must be an object"; EndIf; If Not Payload.Property("id") Or Not Payload.Property("lines") Then Raise "Order id and lines are required"; EndIf; If TypeOf(Payload.id) <> Type("String") Or TypeOf(Payload.lines) <> Type("Array") Then Raise "Invalid order envelope"; EndIf; If IsBlankString(Payload.id) Or Payload.lines.Count() = 0 Then Raise "Empty order"; EndIf; Return Payload; EndFunction