如何告诉DataContractJsonSerializer不包含“__type”属性(How do I tell DataContractJsonSerializer to not include the “__type” property)
我需要将KnownType添加到下面的代码中,以便序列化成功。 当我这样做时,生成的JSON如下所示:
JSON form of Adult with 1 child: {"age":42,"name":"John","children":[{"__type":" Child:#TestJson","age":4,"name":"Jane","fingers":10}]}
我怎么能不包含“__type”:“Child:#TestJson”? 我们在一些查询中返回了数百个这些元素,并且额外的文本会加起来。
完整代码:
using System; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; namespace TestJson { class Program { static void Main(string[] args) { Adult parent = new Adult {name = "John", age = 42}; MemoryStream stream1 = new MemoryStream(); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Adult)); ser.WriteObject(stream1, parent); stream1.Position = 0; StreamReader sr = new StreamReader(stream1); Console.Write("JSON form of Adult with no children: "); Console.WriteLine(sr.ReadToEnd()); Child child = new Child { name = "Jane", age = 4, fingers=10 }; stream1 = new MemoryStream(); ser = new DataContractJsonSerializer(typeof(Child)); ser.WriteObject(stream1, child); stream1.Position = 0; sr = new StreamReader(stream1); Console.Write("JSON form of Child with no parent: "); Console.WriteLine(sr.ReadToEnd()); // now connect the two parent.children.Add(child); stream1 = new MemoryStream(); ser = new DataContractJsonSerializer(typeof(Adult)); ser.WriteObject(stream1, parent); stream1.Position = 0; sr = new StreamReader(stream1); Console.Write("JSON form of Adult with 1 child: "); Console.WriteLine(sr.ReadToEnd()); } } [DataContract] [KnownType(typeof(Adult))] [KnownType(typeof(Child))] class Person { [DataMember] internal string name; [DataMember] internal int age; } [DataContract] class Adult : Person { [DataMember] internal List<Person> children = new List<Person>(); } [DataContract] class Child : Person { [DataMember] internal int fingers; } }
I need to add KnownType to the below code for it to serialize successfully. When I do, the generated JSON is as follows:
JSON form of Adult with 1 child: {"age":42,"name":"John","children":[{"__type":" Child:#TestJson","age":4,"name":"Jane","fingers":10}]}
How do I have it not include the "__type":"Child:#TestJson"? We return hundreds of these elements on some queries and that extra text will add up.
Full code:
using System; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; namespace TestJson { class Program { static void Main(string[] args) { Adult parent = new Adult {name = "John", age = 42}; MemoryStream stream1 = new MemoryStream(); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Adult)); ser.WriteObject(stream1, parent); stream1.Position = 0; StreamReader sr = new StreamReader(stream1); Console.Write("JSON form of Adult with no children: "); Console.WriteLine(sr.ReadToEnd()); Child child = new Child { name = "Jane", age = 4, fingers=10 }; stream1 = new MemoryStream(); ser = new DataContractJsonSerializer(typeof(Child)); ser.WriteObject(stream1, child); stream1.Position = 0; sr = new StreamReader(stream1); Console.Write("JSON form of Child with no parent: "); Console.WriteLine(sr.ReadToEnd()); // now connect the two parent.children.Add(child); stream1 = new MemoryStream(); ser = new DataContractJsonSerializer(typeof(Adult)); ser.WriteObject(stream1, parent); stream1.Position = 0; sr = new StreamReader(stream1); Console.Write("JSON form of Adult with 1 child: "); Console.WriteLine(sr.ReadToEnd()); } } [DataContract] [KnownType(typeof(Adult))] [KnownType(typeof(Child))] class Person { [DataMember] internal string name; [DataMember] internal int age; } [DataContract] class Adult : Person { [DataMember] internal List<Person> children = new List<Person>(); } [DataContract] class Child : Person { [DataMember] internal int fingers; } }
原文:https://stackoverflow.com/questions/17815772
最满意答案
您可以尝试的一个解决方案是:
{ xtype: 'textfield', fieldLabel: 'DNR TYPE', name: 'DNR_TYPE, listeners: { beforerender: function() { this.setValue(Ext.getCmp('dnr-type').getRawValue()); } } }
如果
Ext.getCmp('dnr-type').getRawValue()
返回值,则将其分配给textfield,否则如果它是空字符串,则textfield将包含空值,并且您将看不到textfield中的值。编辑:
当用户从组合框中选择一条记录时,我应该将该值分配给相关的文本字段。
---->你可以在
combo
select
事件上更改textfield的值,如:{ xtype: 'combo' displayField: 'foo', valueField: 'bar', lsiteners: { select: function( combo, records) { var rec = records[0]; // records will contain selected records(1 or more) var textField = // get textfield using Ext.getCmp() textField.setValue(rec.get('modelName')); } } }
现在,有什么方法可以在用户从表单中选择记录时再次刷新/重新加载表单吗?
---->我不清楚这个。 如果你告诉我这个场景,我可以帮你解决这个问题
One solution that you can try is :
{ xtype: 'textfield', fieldLabel: 'DNR TYPE', name: 'DNR_TYPE, listeners: { beforerender: function() { this.setValue(Ext.getCmp('dnr-type').getRawValue()); } } }
If
Ext.getCmp('dnr-type').getRawValue()
returns value, it will be assigned to textfield, otherwise if it is blank string then textfield will contain blank value and you wouldn't see value in textfield.EDIT:
When user select a record from the combobox, I should assign that value to related textfield.
----> you can change value of textfield on
select
event ofcombo
like :{ xtype: 'combo' displayField: 'foo', valueField: 'bar', lsiteners: { select: function( combo, records) { var rec = records[0]; // records will contain selected records(1 or more) var textField = // get textfield using Ext.getCmp() textField.setValue(rec.get('modelName')); } } }
Now, is there any method to refresh/reload form again when user select a record from the form?
----> I am not clear with this. May be I can help you with this if you tell me the scenario
相关问答
更多-
Arasu, 您必须将访问属性指定为“远程”。 在你的情况下,它通过defualt公众。
希望有所帮助。 Arasu, You have to give the access attribute as 'remote'. In your case its by defualt public. hope that hel ... -
你不能调用Sidebar.fetchData因为fetchData不是Sidebar的静态成员,它是一个实例成员。 这意味着你需要一个Sidebar实例来调用fetchData ,例如new Sidebar().fetchData() 。 当然,这不是应该如何使用React组件,并且它不会在所有其他Sidebar实例上设置状态,因此它没有意义。 您要做的是将回调传递给TagUtils组件: export default class TagUtils extends React.Component { ...
-
从类定义外部调用React函数PropType(Calling a React function PropType from outside the class definition)[2023-05-10]
您可以将回调传递给mapRequests方法,而不是尝试直接从props中提取它: class RequestList extends Component { constructor(props) { super(props); this.props.onRequestItemClick = this.props.onRequestItemClick.bind(this) // I'm not sure why your binding to this something yo ... -
这是因为this并不是指回调函数范围内的组件。 你必须使用箭头功能 this.Service.getLoginStatus(response => { if(response.status == 'connected') { this.printHelloWorld(); //throws undefined error } }); 或bind() this.Service.getLoginStatus(function(r ...
-
LOG_AddFunction不是函数。 它是一个类似函数的宏 。 它唯一的“定义”正是你在问题中向我们展示的。 其目的是自动调用LOG_Add并在调用点( __FUNCTION__ )自动传递函数的名称。 无论何时编写LOG_AddFunction() ,这些字符都会被预处理器(与任何其他宏一样)自动替换为LOG_Add(LOG_TYPE_NORMAL, "%s()", __FUNCTION__) 。 这样可以节省每次输入整个LOG_Add ; 而已。 LOG_AddFunction is not a f ...
-
在ctrlPage的模板中,您需要挂钩父控制器的setCurrentItem方法,您可以这样做:
你也需要在调用setCurrentItem时更改并向他传递一个这样的哈希: -
ReactJS使组件状态在组件定义外可用(ReactJS Making Component state available outside Component definition)[2022-09-13]
你不介意使用函数来设置和初始化组件,而是介意使用React条件渲染属性,例如: class DashBoard extends Component { state = { listItemIndex: 0, optionId: 0, }; handleClick = (item, id) => { this.setState({ listItemIndex: item, optionId: id, }); }; render() ... -
只是在同一个文件中是不够的。 定义组件时,需要告诉angularJs要使用哪个控制器,否则它将使用默认控制器(基本上是一个空函数)。 另外,组件使用controller-as语法,所以你不能只调用logout() ,你需要调用$ctrl.logout() 。 例如: // I did the controller as a constructor function, but you could also do a class const MyController = function ($log) { ...
-
您的手风琴组件应该听取父组件的一些输入,以决定它是关闭还是打开。 由于你的手风琴是由* ngFor动态生成的,你最好通过使用我们的用户项来传递数据。 export class AccordionComponent implements OnInit, OnChanges { @Input('accordionExpanded') accordionExpanded = false; ngOnChanges(changes: SimpleChanges) { if(ch ...
-
您可以尝试的一个解决方案是: { xtype: 'textfield', fieldLabel: 'DNR TYPE', name: 'DNR_TYPE, listeners: { beforerender: function() { this.setValue(Ext.getCmp('dnr-type').getRawValue()); } } } 如果Ext.getCmp('dnr-type').getRawValue()返回值,则将其分配给textf ...