Seaky
Seaky

Reputation: 249

Json Deserialize object error

Ok, now I am running into a very weird error. I am trying to deserialize a GameEvent object which is like this:

public class GameEvent {

public Location eventLocation = Location.NoLocation;
public Location targetLocation = Location.NoLocation;
public string eventTriggerName = ""; // Who (Piece or tactic) triggers this event
public string targetTriggerName = ""; // Target name
public int eventPlayerID = -1;
public int targetPlayerID = -1;
public string result = ""; // Piece, Tactic, Trap, Freeze, Move, Kill, Flag
public int amount = 0;

public GameEvent() { Debug.Log("Fuck"); }
public static string ClassToJson(GameEvent gameEvent)
{
    return JsonConvert.SerializeObject(gameEvent);
}
}

When I deserialize it by doing this, however, it is changed weirdly.

public static GameEvent JsonToClass(string json)
{
    Debug.Log(json);
    GameEvent gameEvent = JsonConvert.DeserializeObject<GameEvent>(json);
    Debug.Log(ClassToJson(gameEvent));
    return JsonConvert.DeserializeObject<GameEvent>(json);
}

As you can see from the picture below the eventLocation should be (7,2) but after deserialization it becomes (4,2). And the eventLocation is the only thing that's changed.

string json = "{\"eventLocation\": {\"x\": 7, \"y\": 2}, \"targetLocation\": {\"x\": 4, \"y\": 2} }";
var x = GameEvent.JsonToClass(json);

enter image description here

I have no clue why. This is my Location class

public class Location
{
    public int x = -1;
    public int y = -1;
    public Location(){}
    public Location(int X, int Y)
    {
        x = X;
        y = Y;
    }
    public Location(Location location)
    {
        x = location.x;
        y = location.y;
    }
    public static bool operator !=(Location a, Location b)
    {
        UnityEngine.Debug.Log(a + " " + b);
        return a.x != b.x || a.y != b.y;
    }
    public static Location NoLocation = new Location(-1, -1);
}

I didn't post all the functions of GameEvent and Location class but I posted all the variables they have.

By the way I also met another weird problem with the Location. When I do if(eventLocation != Location.NoLocation), the != operator that I override is actually not comparing eventLocation with Location.NoLocation but eventLocation(yeah itself). So the a and b will always be the same and != will always return me false. I also have no clue why.

Thanks in advance!!!

Upvotes: 1

Views: 966

Answers (1)

Mo Chavoshi
Mo Chavoshi

Reputation: 565

Your problem comes from these two line:

public Location eventLocation = Location.NoLocation;
public Location targetLocation = Location.NoLocation;

It is happening because you are binding both objects to an specific object which is NoLocation. It means both of the eventLocation and targetLocation are pointing to the same object in the heap memory and changing one of them changes the other one as well.

Changing NoLocation to something like this can solve your issue:

public static Location NoLocation { get { return new Location(-1, -1); } }

Upvotes: 3

Related Questions