0

I am passing my json over here as value

var checkthis = JsonConvert.DeserializeObject(value);
string dothis = checkthis.ToString();
Activity   activity = new CompressedJsonActivityTransformer().Deserialize(dothis);

I need to follow my inbuilt CompressedJsonActivityTransformer, But i keep getting a System.NullReferenceException at Activity activity.

I need to implement through this custom deserializer

public class CompressedJsonActivityTransformer:IActivitySerializer,IActivityDeserializer
{ 
    public CompressedJsonActivityTransformer();
    public Activity Deserialize(Stream stream);
    public Activity Deserialize(string jsonData);
    public void Serialize(Activity activity, Stream stream);
}

This is my dynamic object which i get from my db, but need to parse it

{{
  "details": {
    "strength-workout": {
      "$type": "LTF.MyPlan.ActivityUtil.Model.Detail.WorkoutDetail, LTF.MyPlan.ActivityUtil",
      "type": "Workout",
      "workoutId": 94,
      "exerciseDetails": [
        {
          "type": "exercise",
          "exerciseID": 36,
          "sets": [
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            }
          ]
        },
        {
          "type": "exercise",
          "exerciseID": 46,
          "sets": [
            {
              "reps": 20,
              "weight": 4,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 4,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 4,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 4,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 4,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 4,
              "isCompleted": false
            }
          ]
        },
        {
          "type": "exercise",
          "exerciseID": 41,
          "sets": [
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            }
          ]
        },
        {
          "type": "exercise",
          "exerciseID": 39,
          "sets": [
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            },
            {
              "reps": 20,
              "weight": 0,
              "isCompleted": false
            }
          ]
        }
      ]
    }
  },
  "channels": {},
  "startTime": "2016-01-20T09:06:44.375836-06:00",
  "endTime": "2016-01-20T11:07:25.037335-06:00",
  "timerSeconds": 0,
  "performer": {
    "firstName": "Test",
    "nickname": "Test "
  }
}}
Keys
  • 55
  • 1
  • 8
  • checkthis is an object. so checkthis.ToString() isn't going to be the serialized version of that object. You need string dothis = JsonConvert.Serialize(yourObject); – chris-crush-code Apr 27 '17 at 13:30
  • {"Could not cast or convert from System.String to Activity."}. i am getting this error now at at Activity – Keys Apr 27 '17 at 13:40
  • 1
    what does dothis look like when you inspect it? – chris-crush-code Apr 27 '17 at 13:45
  • Please provide a [mcve] that fully demonstrates the problem. Without one this is just a duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/q/4660142/3744182). – dbc Apr 27 '17 at 16:27
  • @dbc. This my dynamic json which i am getting from documentdb – Keys Apr 27 '17 at 19:47
  • And what about the code for `CompressedJsonActivityTransformer.Deserialize()` that is throwing the exception? Without that we cannot help. Try debugging your null reference exception as explained in the linked answer, and if you cannot resolve the problem but can reproduce it consistently, ask again with a full [mcve] See [ask]. Incidentally, your JSON is invalid. Upload it to http://jsonlint.com/ and you will see an error: `Error: Parse error on line 1: Expecting 'STRING', '}', got '{'`. – dbc Apr 27 '17 at 20:00
  • Initially i wanted to parse my json, So first is there any way. I can trim my json, removing curly braces at begining and end. – Keys Apr 27 '17 at 20:03

0 Answers0