Rex_C
Rex_C

Reputation: 2477

Formatting a json object depending on conditional variables in a c# class

I am using PushWoosh in a WCF service to do push notification to 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 = the unique device id and DevicePlatform = either 'android' or 'iOS', I'm creating 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 to have happen is write some sort of conditional statement on the json object with the following:

I'm having a little trouble conceptualizing how to go about this. If anyone could help me with this, I'd appreciate it.

Upvotes: 4

Views: 1527

Answers (2)

CodeMonkey1313
CodeMonkey1313

Reputation: 16031

I misread the question. Here is a semi-complete solution for what I believe you are asking.

Edit: Code sample for solution

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)))
                        ))));

Upvotes: 3

Tim S.
Tim S.

Reputation: 56556

Here's an example of how you might do it:

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);

I think your code could be improved by using classes instead of JObjects (etc.) directly. E.g.

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);

Upvotes: 2

Related Questions