wwDotNetBridge::ToJson

Creates a JSON string from a .NET object or value passed in.

o.ToJson(loDotnetObject)

Return Value

JSON string or null on failure

Parameters

loDotnetObject
A .NET Interop object or simple value like string, number, date, bool...

llFormatted
If .T. forces the JSON to be pretty formatted

Remarks

Only works with .NET Interop objects and simple values (strings, numbers, dates, bools) - FoxPro or other non-.NET COM objects passed in will render empty objects.

Depends on NewtonSoft.Json.dll library present in addition to wwdotnetbridge.dll

Example

loCust = loBridge.CreateInstance("Westwind.WebConnection.TestCustomer")
loCust.Name = "Rick"
loCust.Company = "Westwind"
loCust.Number = 100.21
loCust.Entered = DATETIME()
loCust.Address.Street = "32 Kaiea"
loCust.Address.City = "Paia"

*** Serialize to JSON string
lcJson = loBridge.ToJson(loCust,.t.)
? lcJson

*** Deserialize into a new object entirely
loCust2 = loBridge.FromJson(lcJson,"Westwind.WebConnection.TestCustomer")
? loCust2.Company
? loCust2.Address.City
// JSON result
{
  "Name": "Rick",
  "Company": "Westwind",
  "Entered": "2013-01-21T13:52:32",
  "Number": 100.21,
  "Address": {
    "Street": "32 Kaiea",
    "City": "Paia"
  }
}

See also:

Class wwDotNetBridge | wwJsonSerializer::FromJson | wwJsonSerializer::ToXml

© West Wind Technologies, 2004-2020 • Updated: 05/21/20
Comment or report problem with topic