这是一个非常简单的请求,但我没有找到办法.
我基本上试图在JAXB中设置一个角色,它表示只要遇到空字段,而不是在输出中忽略它,将其设置为空值.所以对于班级:
@XMLRootElement
Class Foo {
Integer num;
Date date;
….
}
Run Code Online (Sandbox Code Playgroud)
如果日期字段为空,则将其编组到XML文件中时,我的输出中没有该元素.我想要做的是包括输出中的所有字段; 如果它们为null,则将它们替换为 - 例如空白.所以输出应该是:
<foo>
<num>123</num>
<date></date>
</foo>
Run Code Online (Sandbox Code Playgroud)
谢谢,
Jalpesh.
如果您尝试编组一个引用没有no-arg构造函数的复杂类型的类,例如:
import java.sql.Date;
@XmlRootElement(name = "Foo")
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
int i;
Date d; //java.sql.Date does not have a no-arg constructor
}
Run Code Online (Sandbox Code Playgroud)
使用JAXB实现作为Java的一部分,如下所示:
Foo foo = new Foo();
JAXBContext jc = JAXBContext.newInstance(Foo.class);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(foo, baos);
Run Code Online (Sandbox Code Playgroud)
JAXB将抛出一个
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions java.sql.Date does not have a no-arg default constructor
Run Code Online (Sandbox Code Playgroud)
现在,我理解为什么JAXB在解组时需要一个无参数的构造函数 - 因为它需要实例化对象.但是为什么JAXB在编组时需要一个无参数的构造函数?
另外,为什么Java的JAXB实现如果该字段为空则抛出异常,并且无论如何都不会被编组?
我在Java的JAXB实现中遗漏了一些东西,或者这些只是糟糕的实现选择?
考虑以下代码:
public enum MyEnum { V1, V2, V3 }
int size = Marshal.SizeOf(typeof(MyEnum));
Run Code Online (Sandbox Code Playgroud)
它抛出异常:
TestConsole.exe中发生了未处理的"System.ArgumentException"类型异常
附加信息:类型'TestConsole.Program + MyEnum'不能作为非托管结构封送; 不能计算有意义的大小或偏移量.
虽然此代码不会抛出异常并size包含4:
public enum MyEnum { V1, V2, V3 }
public struct MyStruct
{
public MyEnum en;
}
int size = Marshal.SizeOf(typeof(MyStruct));
Run Code Online (Sandbox Code Playgroud)
谁能解释为什么.NET框架无法弄清楚enum第一个示例代码中是4个字节?
UPDATE
Marshal.Sizeof() 在这个通用方法中我失败了:
public bool IoControlReadExact<T>(uint ioControlCode, out T output) where T : struct
{
output = new T();
int outBufferSize = Marshal.SizeOf(typeof(T));
IntPtr outBuffer = Marshal.AllocHGlobal(outBufferSize);
if (outBuffer == IntPtr.Zero)
return false;
try
{ …Run Code Online (Sandbox Code Playgroud) 我测试了很多.但我发现那些2没有缺点!
但是看到接受的答案.
GetLastError托管代码是不安全的,因为框架可能在内部"覆盖"最后一个错误.我从来没有遇到任何明显的问题,GetLastError对我来说,.NET Framework足够智能,不会覆盖它.因此,我对该主题有几个问题:
[DllImport("kernel32.dll", SetLastError = true)]该SetLastError属性是否使Framework存储错误代码供使用Marshal.GetLastWin32Error()?GetLastError无法给出正确的结果?Marshal.GetLastWin32Error()吗?public class ForceFailure
{
[DllImport("kernel32.dll")]
static extern uint GetLastError();
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetVolumeLabel(string lpRootPathName, string lpVolumeName);
public static void Main()
{
if (SetVolumeLabel("XYZ:\\", "My Imaginary Drive "))
System.Console.WriteLine("It worked???");
else
{
// the first last error check is fine here:
System.Console.WriteLine(GetLastError());
System.Console.WriteLine(Marshal.GetLastWin32Error());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想将托管代码中的对象传递给WinApi函数IntPtr.它会将此对象传递回托管代码中的回调函数IntPtr.它不是一个结构,它是一个类的实例.
如何转换object到IntPtr和回?
我想编组和解组一个实现Parcelable/来自字节数组的类.我很清楚Parcelable表示不稳定,因此不适用于实例的长期存储.但我有一个用例,我需要序列化一个对象,如果由于内部Android更改而解组失败,它不是一个showstopper.该类已经在实现该Parcelable接口.
给定一个类MyClass implements Parcelable,我如何(ab)使用Parcelable接口进行编组/解组?
我有结构
type tySurvey struct {
Id int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
Run Code Online (Sandbox Code Playgroud)
我json.Marshal在HTML页面中写了JSON字节.jQuery修改name对象中的字段并使用jQueries编码对象JSON.stringify,jQuery将字符串发布到Go处理程序.
id 字段编码为字符串.
发送:{"id":1}收到:{"id":"1"}
问题是json.Unmarshal无法解组JSON,因为id它不再是整数.
json: cannot unmarshal string into Go value of type int64
Run Code Online (Sandbox Code Playgroud)
处理此类数据的最佳方法是什么?我不希望手动转换每个字段.我希望编写紧凑,无错误的代码.
行情也不算太糟糕.JavaScript与int64不兼容.
我想学习使用int64值中的字符串值解组json的简单方法.
我有一些C#代码使用CSharpCodeProvider.CompileAssemblyFromSource在内存中创建一个程序集.在对程序集进行垃圾回收之后,我的应用程序使用的内存比创建程序集之前的内存要多.我的代码在ASP.NET Web应用程序中,但我在WinForm中重复了这个问题.我正在使用System.GC.GetTotalMemory(true)和Red Gate ANTS Memory Profiler来测量增长(使用示例代码大约600个字节).
从我的搜索开始,听起来泄漏来自新类型的创建,而不是来自我所持有的任何对象.我发现的一些网页提到了有关AppDomain的一些内容,但我不明白.有人可以解释这里发生了什么以及如何解决它?
以下是泄漏示例代码:
private void leak()
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
parameters.GenerateExecutable = false;
parameters.ReferencedAssemblies.Add("system.dll");
string sourceCode = "using System;\r\n";
sourceCode += "public class HelloWord {\r\n";
sourceCode += " public HelloWord() {\r\n";
sourceCode += " Console.WriteLine(\"hello world\");\r\n";
sourceCode += " }\r\n";
sourceCode += "}\r\n";
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, sourceCode);
Assembly assembly = null;
if (!results.Errors.HasErrors)
{
assembly = results.CompiledAssembly;
}
}
Run Code Online (Sandbox Code Playgroud)
更新1:此问题可能相关:动态加载和卸载使用CSharpCodeProvider生成的all
更新2:尝试更多地了解应用程序域,我发现: …
c# memory-leaks appdomain marshalling compileassemblyfromsource
我写了MarshalJSON和的自定义版本UnmarshalJSON.我UnmarshalJSON被称为我想要的方式,但我不能让它与之合作MarshalJSON.这是代码总结了我的问题:
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
)
type myStruct struct {
Data string `json:"data"`
}
func (s *myStruct) MarshalJSON() ([]byte, error) {
return []byte(`{"data":"charlie"}`), nil
}
func (s *myStruct) UnmarshalJSON(b []byte) error {
// Insert the string directly into the Data member
return json.Unmarshal(b, &s.Data)
}
func main() {
// Create a struct with initial content "alpha"
ms := myStruct{"alpha"}
// Replace content with "bravo" using custom UnmarshalJSON() …Run Code Online (Sandbox Code Playgroud) 来自文档:
JSON不能表示循环数据结构,Marshal不能处理它们.将循环结构传递给Marshal将导致无限递归.
我遇到过这种情况,导致运行时恐慌.
我想知道的是,是否有人可以提供一个工作程序来演示非恐慌情况,其中json.Marshal返回非零错误.最佳答案显然包括使用的输入.