Formatting a json object depending on condition variables in a C # class

I use PushWoosh in the WCF service to do push notification on either an iOS device or Android. Using the following C # classes:

[DataContract]
public class PendingBadge
{
    [DataMember]
    public int BadgeCount { get; set; }
    [DataMember]
    public List<Uuid> Uuids { get; set; }
}

[DataContract]
public class Uuid
{
    [DataMember]
    public string UuidId { get; set; }
    [DataMember]
    public string DevicePlatform { get; set; }
}

where UuidId = unique identifier of the device and DevicePlatform = either "android" or "iOS", I create a json object to send to PushWoosh.

var totalBadgeCount = new PendingBadge();
//code instantiating Uuid class and creating a list of Uuid from a database
//and adding said list to totalBadgeCount

string pwAuth ="XXXXXXXXXXXXXXXXXXX";
string pwApplication = "XXXXXXXXX";
var json = new JObject(
                 new JProperty("application", pwApplication),
                 new JProperty("auth", pwAuth),
                 new JProperty("notifications",
                       new JArray(
                             new JObject(
                                   new JProperty("send_date", "now"),
                                   new JProperty("ignore_user_timezone", true),
                                   new JProperty("platforms", 1),
                                   new JProperty("ios_badges", totalBadgeCount.BadgeCount),
                                   new JProperty("devices", uuid.UuidId)
                       ))));
 PwCall("createMessage", json);

What I need is to write some kind of conditional statement for the json object with the following:

  • if DevicePlatform is android, set the JProperty platforms to 3 or 1 to iOS.
  • add JProperty "android_header" or JProperty "ios_badges" depending on the platform.
  • merge a comma-separated string into each platform-based uuidId.

, . - , .

+4
2

, :

string pwApplication = "XXXXXXXXX";
bool isAndroid = uuid.DevicePlatform == "android";
var myNotif = new JObject(
                new JProperty("send_date", "now"),
                new JProperty("ignore_user_timezone", true),
                new JProperty("platforms", isAndroid ? 3 : 1),
                new JProperty("devices", uuid.UuidId)
                );
if (isAndroid)
{
    myNotif.Add(new JProperty("android_header", "whatever you put here"));
}
else
{
    myNotif.Add(new JProperty("ios_badges", totalBadgeCount.BadgeCount));
}
myNotif.Add(new JProperty("uuid_list", string.Join(",",
   totalBadgeCount.Uuids.Where(x => x.DevicePlatform == uuid.DevicePlatform)
                        .Select(x => x.UuidId))));
var json = new JObject(
    new JProperty("application", pwApplication),
    new JProperty("auth", pwAuth),
    new JProperty("notifications",
        new JArray(myNotif)));
PwCall("createMessage", json);

, , JObject ( ..) . .

public class Notification
{
    public string send_date { get; set; }
    public bool ignore_user_timezone { get; set; }
    public int platforms { get; set; }
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int? ios_badges { get; set; }
    public string devices { get; set; }
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public string android_header { get; set; }
    public string uuid_list { get; set; }
}

public class MyMessage
{
    public string application { get; set; }
    public string auth { get; set; }
    public List<Notification> notifications { get; set; }
}

string pwApplication = "XXXXXXXXX";
bool isAndroid = uuid.DevicePlatform == "android";
var myNotif =
    new Notification
    {
        send_date = "now",
        ignore_user_timezone = true,
        platforms = isAndroid ? 3 : 1,
        devices = uuid.UuidId
    };
if (isAndroid)
{
    myNotif.android_header = "whatever you put here";
}
else
{
    myNotif.ios_badges = totalBadgeCount.BadgeCount;
}
myNotif.uuid_list = string.Join(",",
   totalBadgeCount.Uuids.Where(x => x.DevicePlatform == uuid.DevicePlatform)
                        .Select(x => x.UuidId));
var myMessage = new MyMessage
{
    application = pwApplication,
    auth = pwAuth,
    notifications = new List<Notification> { myNotif }
};
var json = JsonConvert.SerializeObject(myMessage);
PwCall("createMessage", json);
+2

. - , , .

:

string pwAuth = "XXXXXXXXXXXXXXXXXXX";
string pwApplication = "XXXXXXXXX";
var devicePlatform = "android";
var json = new JObject(
                    new JProperty("application", pwApplication),
                    new JProperty("auth", pwAuth),
                    new JProperty("notifications",
                        new JArray(
                                new JObject(
                                    new JProperty("send_date", "now"),
                                    new JProperty("ignore_user_timezone", true),
                                    new JProperty("platforms", devicePlatform == "android" ? 3 : 1),
                                    new JProperty(devicePlatform == "android" ? "android_header" : "ios_badges", totalBadgeCount.BadgeCount),
                                    new JProperty("devices", String.Join(",", totalBadgeCount.Uuids.Select(u => u.UuidId)))
                        ))));
+2

All Articles