1

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!!!

3
  • Can you post the code for ClassToJson method? btw, that's a very interesting string to write to log. Remember to change it before you push your code. And I noticed that you don't follow naming conventions in Location class's Constructor: it should be X and Y should be in lowercase learn.microsoft.com/en-us/dotnet/standard/design-guidelines/… Commented Jun 4, 2018 at 7:06
  • I think the problem is with static property. Try making it non-static since we need a object to serialize. Commented Jun 4, 2018 at 7:08
  • Please try to reproduce and post a minimal reproducible example. Commented Jun 4, 2018 at 7:16

1 Answer 1

3

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); } }
Sign up to request clarification or add additional context in comments.

2 Comments

This accounts for that last paragraph about the comparison as well, since Location.NoLocation has essentially been overwritten, it looks like it is comparing with itself, and not (-1, -1) as expected, but since Location.NoLocation is the one that has been changed, it is in fact comparing the same instance to itself.
Yeah, since the NoLocation is a static field, it's being instantiated only once, so references won't be broken unless we make recreate it by new keyword.

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.