Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@IntegerType field serialization byte array is correct? #93

Closed
scmp99 opened this issue Feb 18, 2022 · 3 comments
Closed

@IntegerType field serialization byte array is correct? #93

scmp99 opened this issue Feb 18, 2022 · 3 comments

Comments

@scmp99
Copy link

scmp99 commented Feb 18, 2022

import org.indunet.fastproto.FastProto;
import org.indunet.fastproto.annotation.type.IntegerType;
import java.nio.ByteBuffer;
import java.util.Arrays;

public class FastProtoTest {
@IntegerType(0)
private int nameLen = 5;

public static void main(String[] args) {
// Output results: [5, 0, 0, 0]
System.out.println(Arrays.toString(FastProto.toByteArray(new FastProtoTest())));
// toByteArray and parseFrom is right.
System.out.println(
FastProto.parseFrom(FastProto.toByteArray(new FastProtoTest()), FastProtoTest.class)
.nameLen);
// Output results: [0, 0, 0, 5]
System.out.println(Arrays.toString(ByteBuffer.allocate(4).putInt(5).array()));
}
}

@Deng-Ran
Copy link
Member

FastProto uses little endian by default, but ByteBuffer uses big endian.
If you want to use big endian, there are two ways.

  1. All fields are big endian
    import org.indunet.fastproto.annotation.Endian;
    
    @Endian(EndianPolicy.BIG)
    public class FastProtoTest {
    ...
    }
  1. Only field value is big endian.
import org.indunet.fastproto.annotation.Endian;

public class FastProtoTest {
  @Endian(EndianPolicy.BIG)
  int value = 5;
}

@Deng-Ran
Copy link
Member

Thanks for your attention.
FastProto is undergoing a major version change, version 4.0 will ensure simple api, clearer data types, and higher efficiency.

@scmp99
Copy link
Author

scmp99 commented Feb 18, 2022

Thanks for the reply. Got it.

@scmp99 scmp99 closed this as completed Feb 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants