Мне нужно сериализовать объекты (OpenTK.Vector2
), содержащие свойства с геттером, но без сеттера. Я хотел бы, чтобы эти свойства игнорировались вообще, в противном случае я получаю чрезвычайно завышенный JSON от объекта, который имеет две соответствующие части данных (X
и Y
).Не сериализуйте свойства без сеттера
Код:
JsonSerializerSettings settings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
Vector2 v = new Vector2 { X = 1, Y = 0 };
string json = JsonConvert.SerializeObject(v, settings);
производит строку:
{
"X" : 1.0,
"Y" : 0.0,
"Length" : 1.0,
"LengthFast" : 1.0016948,
"LengthSquared" : 1.0,
"PerpendicularRight" : {
"X" : 0.0,
"Y" : -1.0,
"Length" : 1.0,
"LengthFast" : 1.0016948,
"LengthSquared" : 1.0,
"PerpendicularRight" : {
"X" : -1.0,
"Y" : 0.0,
"Length" : 1.0,
"LengthFast" : 1.0016948,
"LengthSquared" : 1.0,
"PerpendicularRight" : {
"X" : 0.0,
"Y" : 1.0,
"Length" : 1.0,
"LengthFast" : 1.0016948,
"LengthSquared" : 1.0
}
},
"Yx" : {
"X" : -1.0,
"Y" : 0.0,
"Length" : 1.0,
"LengthFast" : 1.0016948,
"LengthSquared" : 1.0,
"PerpendicularRight" : {
"X" : 0.0,
"Y" : 1.0,
"Length" : 1.0,
"LengthFast" : 1.0016948,
"LengthSquared" : 1.0
}
}
},
"PerpendicularLeft" : {
"X" : 0.0,
"Y" : 1.0,
"Length" : 1.0,
"LengthFast" : 1.0016948,
"LengthSquared" : 1.0,
"PerpendicularLeft" : {
"X" : -1.0,
"Y" : 0.0,
"Length" : 1.0,
"LengthFast" : 1.0016948,
"LengthSquared" : 1.0,
"PerpendicularLeft" : {
"X" : 0.0,
"Y" : -1.0,
"Length" : 1.0,
"LengthFast" : 1.0016948,
"LengthSquared" : 1.0
},
"Yx" : {
"X" : 0.0,
"Y" : -1.0,
"Length" : 1.0,
"LengthFast" : 1.0016948,
"LengthSquared" : 1.0
}
}
},
"Yx" : {
"X" : 0.0,
"Y" : 1.0,
"Length" : 1.0,
"LengthFast" : 1.0016948,
"LengthSquared" : 1.0,
"PerpendicularLeft" : {
"X" : -1.0,
"Y" : 0.0,
"Length" : 1.0,
"LengthFast" : 1.0016948,
"LengthSquared" : 1.0,
"PerpendicularLeft" : {
"X" : 0.0,
"Y" : -1.0,
"Length" : 1.0,
"LengthFast" : 1.0016948,
"LengthSquared" : 1.0
},
"Yx" : {
"X" : 0.0,
"Y" : -1.0,
"Length" : 1.0,
"LengthFast" : 1.0016948,
"LengthSquared" : 1.0
}
}
}
}
Как я могу получить сериализатор игнорировать эти другие свойства?
На самом деле мне трудно описать логику вывода. Дерево для 'PerpendicularRight' включает в себя' PerpendicularLeft' и наоборот. – AbleArcher