Thought
Thought

Reputation: 5846

Save json information in objects in windows store app

In a windows store app project i get a Json with several items from a webserver that looks like this http://paste2.org/dJP85EOJ (this is one item)

i then have a method that reads the json information and creates a object with that information

public async Task createMeet(IJsonValue item, List<Meeting> listMeetings)
    {

        Meeting meeting = new Meeting();
        List<MeetingInvitee> invitees = new List<MeetingInvitee>();
        List<MeetingPoint> meetingPoints = new List<MeetingPoint>();
        MeetingsDB MeetForDB = new MeetingsDB();
        GappService gappservice = new GappService();



        try
        {

            JsonObject newMeet;
            JsonObject.TryParse(item.Stringify(), out newMeet);

            if (newMeet.ContainsKey("_id"))
                if (newMeet["_id"].ValueType != JsonValueType.Null)
                {
                    meeting.Id = newMeet["_id"].GetString();

                    MeetForDB.Id = newMeet["_id"].GetString();
                }
            if (newMeet.ContainsKey("name"))
                if (newMeet["name"].ValueType != JsonValueType.Null)
                {
                    meeting.Name = newMeet["name"].GetString();
                    MeetForDB.Name = newMeet["name"].GetString();
                }
            if (newMeet.ContainsKey("description"))
                if (newMeet["description"].ValueType != JsonValueType.Null)
                {
                    meeting.Description = newMeet["description"].GetString();
                    MeetForDB.description = newMeet["description"].GetString();
                }
            if (newMeet.ContainsKey("meetingType"))
                if (newMeet["meetingType"].ValueType != JsonValueType.Null)
                {
                    meeting.MeetingType = newMeet["meetingType"].GetString();
                    MeetForDB.meetingType = newMeet["meetingType"].GetString();
                }
            if (newMeet.ContainsKey("meetingStatus"))
                if (newMeet["meetingStatus"].ValueType != JsonValueType.Null)
                {
                    meeting.Status = newMeet["meetingStatus"].GetString();
                    MeetForDB.meetingStatus = newMeet["meetingStatus"].GetString();
                }
            if (newMeet.ContainsKey("organizer"))
                if (newMeet["organizer"].ValueType != JsonValueType.Null)
                {
                    meeting.Organizer = newMeet["organizer"].GetString();
                    MeetForDB.organizer = newMeet["organizer"].GetString();
                }
            if (newMeet.ContainsKey("alternativeOrganizer"))
                if (newMeet["alternativeOrganizer"].ValueType != JsonValueType.Null)
                {
                    meeting.AlternativeOrganizer = newMeet["alternativeOrganizer"].GetString();
                    MeetForDB.alternativeOrganizer = newMeet["alternativeOrganizer"].GetString();
                }
            if (newMeet.ContainsKey("organization"))
                if (newMeet["organization"].ValueType != JsonValueType.Null)
                {
                    meeting.Organization = newMeet["organization"].GetString();
                    MeetForDB.organization = newMeet["organization"].GetString();
                }

            if (newMeet.ContainsKey("startDate"))
                if (newMeet["startDate"].ValueType != JsonValueType.Null)
                {
                    meeting.StartDate = GetDateTime(newMeet["startDate"].GetNumber());
                    MeetForDB.startDate = newMeet["startDate"].GetNumber();
                }

                ....
}

its quite a long method with some nested foreachs inside it, and because of that it takes some seconds to create the final object.

What i would like to know is if there is a way of passing the json information to a Object directly without having to read 1 field at a time.

Upvotes: 0

Views: 47

Answers (1)

Matt Lacey
Matt Lacey

Reputation: 65556

Typically I'd point you to something like json.net but the complexity of your JSON is more than it can handle.

Instead I'd look to have the server return less or format it in a way that is easier to consume. The intent being that a change in one place on the server can make things faster for all who are consuming it.
If you don't control the server then you could consider creating your own server that sits between the app and the one providing the content. Yes, there can be additional development/maintenance/running costs associated with this and it will be up to you to decide if they are worth it for the benefits (smaller, faster downloads; simpler processing code; better caching; faster code/app; and more...) they bring.

Upvotes: 1

Related Questions