Objects in Map<String,Object> and other complex containers sometimes disappear
See original GitHub issueI am using pyjnius to pass nested Python dictionaries and lists to Java Sometimes the content of Map<String,Object> and ArrayList<Object> disappears.
It usually goes okay with simple one-dimensional lists and dictionaries filled with strings and integers. But when structures get more complex sometimes data goes missing. It doesn’t always go wrong, one day a certain Python object maps fine to Java, the other day the resulting Java-objects misses items or is completely empty.
I’m using the following versions: Python 2.7.6 Cython 0.23.4 java version “1.7.0_95” OpenJDK Runtime Environment (IcedTea 2.6.4) (7u95-2.6.4-0ubuntu0.14.04.1) OpenJDK 64-Bit Server VM (build 24.95-b01, mixed mode) Tried jnius 1.0.2 and 1.1-dev
import jnius
from collections import Iterable, Mapping
import gc
# Disable the Python garbage-collector, just to be sure
gc.disable()
# Java DataTypes
jMap = jnius.autoclass('java.util.HashMap')
jArrayList = jnius.autoclass('java.util.ArrayList')
jInt = jnius.autoclass('java.lang.Integer')
jLong = jnius.autoclass('java.lang.Long')
jFloat = jnius.autoclass('java.lang.Float')
jDouble = jnius.autoclass('java.lang.Double')
jString = jnius.autoclass('java.lang.String')
class JavaNumber(object):
'''
Convert int/float to their corresponding Java-types based on size
'''
def __call__(self, obj):
if isinstance(obj, int):
if obj <= jInt.MAX_VALUE:
return jInt(obj)
else:
return jLong(obj)
elif isinstance(obj, float):
if obj < jFloat.MAX_VALUE:
return jFloat(obj)
else:
return jDouble(obj)
# Map between Python types and Java
javaTypeMap = { int: JavaNumber(),
str: jString,
float: JavaNumber() }
def mapObject(data):
'''
Recursively convert Python object to Java Map<String, Object>
:param data:
'''
try:
if type(data) in javaTypeMap:
# We know of a way to convert type
return javaTypeMap[type(data)](data)
elif isinstance(data, jnius.MetaJavaClass):
# It's already a Java thingy, or None
return data
elif isinstance(data, Mapping):
# Object is dict-like
map = jMap()
for key, value in data.iteritems():
map.put(str(key), mapObject(value))
return map
elif isinstance(data, Iterable):
# Object is list-like
array = jArrayList()
for item in data:
i = mapObject(item)
array.add(i)
return array
else:
# Convert it to a String
return jString(str(data))
except:
print 'Failed to map Python-object to Java!'
print str(data)
raise
goes_okay = {'list': {3: 4, 5: 6},
4 : 5 }
misses_dict_item = {'list': [1, 2, 7],
'int': 3,
4: 5,
'dict': {2: 3} }
empty = {'access_log': [{'stored_proc': 'getsomething'},
{'uses': [{'usedin': 'some->bread->crumb'},
{'usedin': 'something else here'},
{'stored_proc': 'anothersp'}]},
{'uses': [{'usedin': 'blahblah'}]}],
'reporting': [{'stored_proc': 'reportingsp'},
{'uses': [{'usedin': 'breadcrumb'}]}]}
print mapObject(goes_okay).toString()
print mapObject(misses_dict_item).toString()
print mapObject(empty).toString()
<bountysource-plugin>
---
Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/32616881-objects-in-map-string-object-and-other-complex-containers-sometimes-disappear?utm_campaign=plugin&utm_content=tracker%2F77133&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F77133&utm_medium=issues&utm_source=github).
</bountysource-plugin>Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:8 (3 by maintainers)
Top Results From Across the Web
java - Convert Map<String,Object> to Map<String,String>
If your Objects are containing of Strings only, then you can do it like this:
Read more >Container Classes, C++ FAQ - Standard C++
Why doesn't C++ provide heterogeneous containers? How can I build a heterogeneous <favorite container> of objects of different types? Why can't I assign...
Read more >Built-in Types — Python 3.11.1 documentation
Objects of different types, except different numeric types, never compare equal. The == operator is always defined but for some object types (for...
Read more >7: STL Containers & Iterators
A container class describes an object that holds other objects. ... Each string is placed in the set using insert( ), and the...
Read more >Understanding Map and Set Objects in JavaScript | DigitalOcean
Maps accept any data type as a key, and do not allow duplicate key values. We can demonstrate this by creating a map...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
here is the reference to the other issue: https://github.com/kivy/pyjnius/issues/345
That’s awesome! I will have a look. From my naive knowledge about Python, it appears to an issue with the garbage collection. Basically, the second you leave the current scope the object gets deleted. It feels like this might be connected to the bug with the refcounter increase reported in another bug. I am currently at mobile but I will link the issue tomorrow.
On Wed, Nov 28, 2018, 21:03 Curtis Rueden <notifications@github.com wrote: