JAXB marshalling XMPP stanzas

How about the following?: Create a custom XMLStreamWriter that will treat all namespace declarations as default namespaces, and then marshal to that: ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLOutputFactory xof = XMLOutputFactory.newFactory(); XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out); xsw = new MyXMLStreamWriter(xsw); m.marshal(iq, xsw); xsw.close(); MyXMLStreamWriter import java.util.Iterator; import javax.xml.namespace.NamespaceContext; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; public class MyXMLStreamWriter implements … Read more

C++ .NET convert System::String to std::string

There is cleaner syntax if you’re using a recent version of .net #include “stdafx.h” #include <string> #include <msclr\marshal_cppstd.h> using namespace System; int main(array<System::String ^> ^args) { System::String^ managedString = “test”; msclr::interop::marshal_context context; std::string standardString = context.marshal_as<std::string>(managedString); return 0; } This also gives you better clean-up in the face of exceptions. There is an msdn article … Read more

How to marshall and unmarshall a Parcelable to a byte array with help of Parcel?

First create a helper class ParcelableUtil.java: public class ParcelableUtil { public static byte[] marshall(Parcelable parceable) { Parcel parcel = Parcel.obtain(); parceable.writeToParcel(parcel, 0); byte[] bytes = parcel.marshall(); parcel.recycle(); return bytes; } public static Parcel unmarshall(byte[] bytes) { Parcel parcel = Parcel.obtain(); parcel.unmarshall(bytes, 0, bytes.length); parcel.setDataPosition(0); // This is extremely important! return parcel; } public static <T> … Read more

Reading a C/C++ data structure in C# from a byte array

From what I can see in that context, you don’t need to copy SomeByteArray into a buffer. You simply need to get the handle from SomeByteArray, pin it, copy the IntPtr data using PtrToStructure and then release. No need for a copy. That would be: NewStuff ByteArrayToNewStuff(byte[] bytes) { GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); try … Read more