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

Works only in the Commercial versions of wwDotnetBridge (West Wind tools)

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, 1996-2024 • Updated: 10/02/24
Comment or report problem with topic