vimt / MaxMind-DB-Writer-python

MIT License
33 stars 14 forks source link

Cannot serialize a number field and read it in Java as a long type. #4

Closed stepanov1997 closed 3 months ago

stepanov1997 commented 8 months ago

I have written to an MMDB file in Python:

writer = MMDBWriter()
writer.database_type = 'GeoIP2-ISP'
writer.insert_network(IPSet(['1.1.0.0/24', '1.1.1.0/24']), {'city': 'CITY', 'isp': 'ISP'})
writer.insert_network(IPSet(['8.8.8.0/24']), {
     "isp": 'Google',
     "organization": 'Google',
     "ip_address": '8.8.8.0',
     "autonomous_system_organization": 'GOOGLE',
     "autonomous_system_number": '15169'
})
writer.to_db_file(filename)

I have used the file in a Java application and encountered an error while deserializing data:

Caused by: java.lang.IllegalArgumentException: argument type mismatch
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:65)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
    at com.maxmind.db.Decoder.decodeMapIntoObject(Decoder.java:436)
    ... 160 more
Caused by: java.lang.ClassCastException: Cannot cast java.lang.String to java.lang.Long
    at java.base/java.lang.Class.cast(Class.java:4067)
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
    ... 163 more

When I used the integer value 15169, I received a similar message: 'Cannot cast java.lang.Integer to java.lang.Long'.

How can I mark a field to be parsed as long?

vimt commented 3 months ago

Project has update. u can update by

pip install -U git+https://github.com/VimT/MaxMind-DB-Writer-python

And try this

from mmdb_writer import MMDBWriter
writer = MMDBWriter(int_type='i32')
timmey09 commented 3 months ago

Just to let you know: The merge request requires python 3.10 to work as the usage of Union[float | Decimal] requires it.

def init(self, value: Union[float | Decimal]): TypeError: unsupported operand type(s) for |: 'type' and 'type'

Maybe it makes sense to point this out somewhere in the Readme and update the pyproject.toml.

vimt commented 3 months ago

@timmey09 Thanks for finding the bug. The correct usage of typing.Union should be Union[float, Decimal]