0

i have a console application that gets data from different API's.

The data that is received is mapped to a custom class. This class is later serialized into a json string. Once this is done I use urbanShip to push it to a mobile app.

My problem:

I only want the message property to bee seen once the push reaches the application. All others properties should be included but they need to be hidden. Other properties are needed in order for the app to get info about a specific object with a Id.

I have tried: I tried using [JsonIgnore] and [DataMember] annotations on the properties that i want shown/hidden. This however did not work and I am wondering how can i accomlish this?

My code:

The class that I am currently serializing and pushing:

[DataContract]
public class SubscriberPush
{
    [DataMember]
    public string TrainId { get; set; }
    public string Message { get; set; }
    public string Title { get; set; }
    public string From { get; set; }
    public string To { get; set; }
    public bool LateTrain { get; set; }
    public bool Canceled { get; set; }
}

setting the data:

pushSub.TrainId = trainAnnouncement.AdvertisedTrainIdent;
pushSub.Title = "the train is late";
pushSub.LateTrain = true;
pushSub.Message = "train is " + span.Minutes + " min late";

string json = Parser.ConvertToJson<SubscriberPush>(pushSub);

UrbanAirship.Push(ee.DeviceToken, json);

serializer:

public static string ConvertToJson<T>(T obj)
{
    var json = new JavaScriptSerializer().Serialize(obj);

    return json.ToString(); 
}

JSON:

{"TrainId":"1036","Message":"train is 8 min late","Title":"the Train is late","From":null,"To":null,"LateTrain":true,"Canceled":false}
1
  • What do you mean but they need to be hidden? They are either sent or they are not. Commented Jun 17, 2016 at 11:21

1 Answer 1

1

You can replace your JavaScriptSerializer convert by DataContractJsonSerializer or by json.net serializer (the best)

A little example using DataMember / DataContract attributes :

http://www.newtonsoft.com/json/help/html/DataContractAndDataMember.htm

Sign up to request clarification or add additional context in comments.

4 Comments

This gave me half the answer it would seem. I want to HIDE the other paramaters not exclud them. Any ideas? thanks for the fast reply
I think I misunderstood something, how do you want to hide your properties ? Do you want to send them as null value or encrypted ?
it's not clear what you want to achieve. What do you mean by "hiding the properties" ? And why do you want to hide them? Hide them from whom ?
Well imagine a webapi that has a form and has hiddenfor properties on the page. They shold not be shown but need to be there for other functions. I am trying to send a json object that gets handeld by the app and presents only the data that for example does not have the class "hidden". I am thinking of moving this logic to the app developer but was curious if this could be achived.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.