JSON <nonserialized()>无效VB.NET?

时间:2015-05-16 19:31:57

标签: json vb.net serialization casting

我在使用VB.NET进行的一些JSON序列化方面遇到了一些麻烦。

基本上,我有两节课。

MenuItemDataLong 并且 MenuItemDataShort

MenuItemDataLong 继承 MenuItemDataShort

代码:

MenuItemDataShort.vb

<Serializable()>
Public Class MenuItemDataShort
    Public Display_Name As String = ""
    Public Display_Description As String = ""
    Public Display_Price As Single = 0.0

    Public SELECTED_NAME As Integer = 0
    Public SELECTED_PRICE As Integer = 0

    Public WEB_IMAGE As String = ""
    Public WEB_IMAGE_WIDTH As Integer = 0
    Public WEB_IMAGE_HEIGHT As Integer = 0

    Public PAGE_NUM As Integer = 0
    Public CELL_ID As Integer = 0

    Public SHOW_BMP_FILE As Boolean = False
    Public SHOW_DESCRIPT As Boolean = False
    Public SHOW_PRICE As Boolean = False
    Public SHOW_LONG_DESC As Boolean = False

    Public LONG_DESC_FONT_NAME As String
    Public LONG_DESC_FONT_BOLD As Boolean
    Public LONG_DESC_FONT_UNDERLINE As Boolean
    Public LONG_DESC_FONT_ITALIC As Boolean
    Public LONG_DESC_FONT_COLOR_R As Integer
    Public LONG_DESC_FONT_COLOR_G As Integer
    Public LONG_DESC_FONT_COLOR_B As Integer
    Public LONG_DESC_FONT_SIZE As Integer

    Public DESC_FONT_NAME As String
    Public DESC_FONT_BOLD As Boolean
    Public DESC_FONT_UNDERLINE As Boolean
    Public DESC_FONT_ITALIC As Boolean
    Public DESC_FONT_COLOR_R As Integer
    Public DESC_FONT_COLOR_G As Integer
    Public DESC_FONT_COLOR_B As Integer
    Public DESC_FONT_SIZE As Integer

    Public PRICE_FONT_NAME As String
    Public PRICE_FONT_BOLD As Boolean
    Public PRICE_FONT_UNDERLINE As Boolean
    Public PRICE_FONT_ITALIC As Boolean
    Public PRICE_FONT_COLOR_R As Integer
    Public PRICE_FONT_COLOR_G As Integer
    Public PRICE_FONT_COLOR_B As Integer
    Public PRICE_FONT_SIZE As Integer

    Public DESC_X As Integer
    Public DESC_Y As Integer
    Public PRICE_X As Integer
    Public PRICE_Y As Integer
    Public IMAGE_X As Integer
    Public IMAGE_Y As Integer
    Public LONG_DESC_X As Integer
    Public LONG_DESC_Y As Integer
End Class

MenuItemDataLong.vb

<Serializable()>
Public Class MenuItemDataLong
    Inherits MenuItemDataShort

    <NonSerialized()>
    Public DESCRIPT As String

    <NonSerialized()>
    Public OO_Alias As String

    <NonSerialized()>
    Public LONG_DESCRIPTION As String

    <NonSerialized()>
    Public RECIPE_DESCRIPTION As String

    <NonSerialized()>
    Public BMP_FILE As String

    <NonSerialized()>
    Public PRICE1, PRICE2, PRICE3, PRICE4, PRICE5, PRICE6, PRICE7, PRICE8, PRICE9, PRICE10, PRICE11, PRICE12, PRICE13, PRICE14 As Single

End Class

然后我将 MenuItemDataLong 对象存储在某个地方。 MenuItemDataLong 类中的多余数据在应用程序中是必需的,但在序列化文件后不是必需的。所以,我尝试使用 MenuItemDataLong 并将其强制转换为 MenuItemDataShort 并对其进行序列化。但是,即使使用 NonSerialized()标记,我也会在其中使用 MenuItemDataLong 中的所有数据。

任何人都知道我做错了什么?..

编辑: 如果有人好奇,这就是我序列化的方式:

Dim sItemData As MenuItemDataShort = CType(ItemData, MenuItemDataShort)
Dim Serializer As New System.Web.Script.Serialization.JavaScriptSerializer
Dim Serialized As String = Serializer.Serialize(sItemData)

1 个答案:

答案 0 :(得分:2)

您基本上使用了错误的属性。来自MSDN - NonSerialized

  

使用BinaryFormatter或SoapFormatter类序列化对象时,请使用NonSerializedAttribute属性来阻止字段序列化。

它没有提到JavaScriptSerializer。还有另一个属性:<ScriptIgnore>似乎有效。使用简化类以及实际属性和字段的混合:

Imports System.Web.Script.Serialization

<Serializable()>
Public Class MenuItemShort   
    Public Property Name As String = ""
    Public Description As String = ""
End Class

<Serializable()>
Public Class MenuItemLong
    Inherits MenuItemShort

    <ScriptIgnore>
    Public Property Foo As String
    <ScriptIgnore>
    Public Bar As String

    Public Property Value As Integer
End Class

测试代码:

Dim mi As New MenuItemLong With {.Name = "foo",
                                 .Description = "a foo item",
                                 .Foo = "ghost in the machine",
                                 .Bar = "bar",
                                 .Value = 42}

Dim miS As MenuItemShort = DirectCast(mi, MenuItemShort)

Dim jser As New System.Web.Script.Serialization.JavaScriptSerializer
Dim jss As String = jser.Serialize(miS)

Console.WriteLine(jss)

输出:

  

{“Description”:“foo item”,“Value”:42,“Name”:“foo”}

使用NewtonSoft的JSON.Net还有其他方法可以忽略某些属性。