Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
#Json-CPP A better json library.
pip install json-cpp
After installing the package, try the following python script:
from json_cpp import JsonObject
myobject = JsonObject(name="German Espinosa",age=41,weight=190.0)
print("name:", myobject.name, type(myobject.name).__name__)
print("age:", myobject.age, type(myobject.age).__name__)
print("weight:", myobject.weight, type(myobject.weight).__name__)
print(myobject)
output
name: German Espinosa str
age: 41 int
weight: 190.0 float
{"name":"German Espinosa","age":41,"weight":190.0}
To quickly load json data into objects, use the load command:
from json_cpp import JsonObject
myobject = JsonObject.load("{\"name\":\"German Espinosa\",\"age\":41,\"weight\":190.0}")
print("name:", myobject.name, type(myobject.name).__name__)
print("age:", myobject.age, type(myobject.age).__name__)
print("weight:", myobject.weight, type(myobject.weight).__name__)
output
name: German Espinosa str
age: 41 int
weight: 190.0 float
You can easily format data, even in complex json hierarchical structures:
from json_cpp import JsonObject
myobject = JsonObject.load("{\"name\":\"German Espinosa\",\"age\":41,\"weight\":190.0,\"place_of_birth\":{\"country\":\"Argentina\",\"city\":\"Buenos Aires\"}}")
print(myobject.format("{name} was born in {place_of_birth.city}, {place_of_birth.country}"))
output
German Espinosa was born in Buenos Aires, Argentina
A powerful way to read and write json is to pre-define the structure of the data. This creates standarized data samples that are easire to be consumed by other tools. To pre-define structure of a json object, you need to create your own custom class extending the JsonObject:
from json_cpp import JsonObject
class MyJsonClass(JsonObject):
def __init__(self, name="", age=0, weight=0.0):
self.name = name
self.age = age
self.weight = weight
myobject = MyJsonClass('German Espinosa', 41, 190.0)
json_string = str(myobject)
print(json_string)
output
{"name":"German Espinosa","age":41,"weight":190.0}
You can also load values from a json string directly into an existing custom JsonObject:
from json_cpp import JsonObject
class MyJsonClass(JsonObject):
def __init__(self, name="", age=0, weight=0.0):
self.name = name
self.age = age
self.weight = weight
myobject = MyJsonClass('German Espinosa', 41, 190.0)
myobject.parse("{\"name\":\"Benjamin Franklin\",\"age\":84,\"weight\":195.5}")
json_string = str(myobject)
print(json_string)
output
{"name":"Benjamin Franklin","age":84,"weight":195.5}
All objects with type MyJsonClass will produce perfectly formed json when converted to string. If you need to retrieve the json string representing the object:
from json_cpp import JsonObject
class MyJsonClass(JsonObject):
def __init__(self, name="", age=0, weight=0.0):
self.name = name
self.age = age
self.weight = weight
myobject = MyJsonClass('German Espinosa', 41, 190.0)
json_string = str(myobject)
print (json_string)
output
{"name":"German Espinosa","age":41,"weight":190.0}
You can create instances of your json objects from strings containing a correct json representation:
from json_cpp import JsonObject
class MyJsonClass(JsonObject):
def __init__(self, name="", age=0, weight=0.0):
self.name = name # string
self.age = age # int
self.weight = weight # float
json_string = "{\"name\":\"German Espinosa\",\"age\":41,\"weight\":190.0}"
myobject = MyJsonClass.parse(json_string)
print("name:", myobject.name, type(myobject.name).__name__)
print("age:", myobject.age, type(myobject.age).__name__)
print("weight:", myobject.weight, type(myobject.weight).__name__)
output
name: German Espinosa str
age: 41 int
weight: 190.0 float
note: all members are populated with the right values using the same data type declared in the default constructor of the class
You can create complex structures with nested objects:
from json_cpp import JsonObject
class Person(JsonObject):
def __init__(self, name="", age=0):
self.name = name
self.age = age
class Transaction(JsonObject):
def __init__(self, buyer=None, seller=None, product="", amount=0.0):
self.buyer = buyer if buyer else Person()
self.seller = seller if seller else Person()
self.product = product
self.amount = amount
mytransaction = Transaction(Person("German Espinosa", 41), Person("Benjamin Franklin", 84), "kite", 150.5)
print (mytransaction)
output
{"buyer":{"name":"German Espinosa","age":41},"seller":{"name":"Benjamin Franklin","age":84},"product":"kite","amount":150.5}
You can load full lists with values from a json string to a JsonList:
from json_cpp import JsonObject, JsonList
fibonacci = JsonList(list_type=int)
json_string = "[1,1,2,3,5,8,13,21]"
fibonacci.parse(json_string)
You can also load a list of json objects:
from json_cpp import JsonObject, JsonList
class Person(JsonObject):
def __init__(self, name="", surname=""):
self.name = name
self.surname = surname
person_list = JsonList(list_type=Person)
json_string = "[{\"name\":\"german\",\"surname\":\"espinosa\"},{\"name\":\"benjamin\",\"surname\":\"franklin\"}]"
person_list.parse(json_string)
Lists can also be used as members of other objects:
from json_cpp import JsonObject, JsonList
class Person(JsonObject):
def __init__(self):
self.name = ""
self.surname = ""
self.languages = JsonList(list_type=str)
person = Person.parse("{\"name\":\"German\",\"surname\":\"Espinosa\", \"languages\":[\"english\",\"spanish\",\"portuguese\"]}")
print(person)
output
{"name":"German","surname":"Espinosa","languages":["english","spanish","portuguese"]}
FAQs
a better json library
We found that json-cpp demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.