Sugar Json example?

Hello?

I downloaded latest beta and add references with “sugar, sugar.data”

in github test example of json, they references sugar.data.JSON

however, in my ide, it says there is no such thing…

i think sugar on github and the one ships with beta are different.

should i download github version? or should i stick to off the shelf shipped version?

if the shipped version is latest one, can i have a example for JSON features?

thank you.

It’s Sugar.Json, not Sugar.Data.Json.

Here’s a sample snippet from my Curaçao Weather app. The code in question is used on .NET, iOS and Android:

import Sugar.Json

public class SummaryData: WeatherData {
	public var Condition: String?
	public var Temperature: Double?
	public var HeatIndex: Double?
        ....
	public var WarningEnglish: String?
	public var WeatherSummaryTextEnglish: String?
        ....

	init(json: JsonNode) {
		super.init(json: json)
		Condition = json["Condition"]?.StringValue
		Temperature = json["Temperature"]?.FloatValue
		HeatIndex = json["HeatIndex"]?.FloatValue

		if let details = json["Details"] as? JsonObject, english = details["English"] as? JsonObject {
			WarningEnglish = english["Warning"]?.StringValue
			WeatherSummaryTextEnglish = english["Weather"]?.StringValue
                        ....
		}
	}
	
	override func ToJson() -> JsonDocument {
		let result = JsonDocument()
		result.Root["Condition"] = JsonStringValue.Create(Condition)
		result.Root["Temperature"] = JsonFloatValue.Create(Temperature)
		result.Root["HeatIndex"] = JsonFloatValue.Create(HeatIndex)
		
		let textsEnglish = JsonObject()
		result.Root["Details"] = JsonObject()
		result.Root["Details"]!["English"] = textsEnglish
		
		if let WarningEnglish = WarningEnglish, WarningColor = WarningColor {
			textsEnglish["Code\(WarningColor)"] = JsonStringValue(WarningEnglish) // legacy, remove
			textsEnglish["Warning"] = JsonStringValue(WarningEnglish)
			textsEnglish["WarningColor"] = JsonStringValue(WarningColor)
		}
		if let WeatherSummaryTextEnglish = WeatherSummaryTextEnglish {
			textsEnglish["Weather"] = JsonStringValue(WeatherSummaryTextEnglish)
		}
                ...
		return result
	}
}

hth.

1 Like

Wow thank you.

It is nice to see the some of code of Curaçao weather app :smiley:

1 Like