I can not solve the error: TypeError: Object of type method-wrapper is not JSON serializable

1
# Import(s) #
import json
from chat import message_handler as me
import random
#############

mind = {"message": "null","mID": 0,"reply": "null"}


def learn(m):
    getMessage = m
    getReply = me.reply

    strReply = getReply.__str__

    mind["mID"] = random.randint(0,10000000)
    mind["message"] = getMessage
    mind["message"] = strReply

    with open("../data/TEMP.json", "w") as t:
        json.dump(mind, t)
    saveLearned()

def saveLearned():
    temp = open("../data/TEMP.json")
    brain = open("../data/conv.json")
    with open("../data/conv.json", "w") as f:
        json.dump(temp, brain)

I have this code, it is a module to "learn" new answers to x commands. First save the new "fragment" to write in a temporary file, then pass it to the final file (so I have a small record and I keep a little more informed of small errors that may occur without having to search the final file). The problem is that when I compile it, it returns the error "TypeError: Object of type method-wrapper is not serializable JSON". I have been looking for a solution for a while after having tried many things ... but I can not solve it correctly. Could someone help me a little? At least guide me a little, I'm pretty lost with this error and I can not find a convenient solution.

    
asked by guskikalola 17.11.2018 в 22:37
source

2 answers

0

you are passing something that is not a json since you pass the variable temp that is not more than the file not what it contains for it you use the method read ()

# Import(s) #
import json
from chat import message_handler as me
import random
#############

mind = {"message": "null","mID": 0,"reply": "null"}


def learn(m):
    getMessage = m
    getReply = me.reply

    strReply = getReply.__str__

    mind["mID"] = random.randint(0,10000000)
    mind["message"] = getMessage
    mind["message"] = strReply

    with open("../data/TEMP.json", "w") as t:
        json.dump(mind, t)
    saveLearned()

def saveLearned():
    #leer el archivo con el metodo read()
    temp = open("../data/TEMP.json").read()
    #brain = open("../data/conv.json")
    with open("../data/conv.json", "w") as f:
        json.dump(temp, f)
    
answered by 18.11.2018 / 00:00
source
1

First the explanation of the error . It tells you TypeError: Object of type method-wrapper is not JSON serializable which basically means that one of the dictionary fields you want to dump as JSON is not "serializable", that is, it is not a list, string, number, or other dictionary or something that can be treated as such . Moreover, it tells you that the problem is that it is of type method-wrapper , that is, a function (in particular, a function not implemented in your program, but imported from a module, or part of the builtins of python, which is programmed in C).

Second the place where you make your mistake , which is on the line that says:

strReply = getReply.__str__

That assigns to strReply the attribute __str__ of getReply , but that attribute is not the representation as a string, which is what it seems you thought. It is a method that converts in string to object getReply . But in your assignment you are not invoking that method (because the parentheses would be missing as in getReply.__str__() ) and therefore what you assign is directly a reference to the method. Precisely the method-wrapper of which then complains json.dump() .

Resolved by adding the parentheses that were missing in the call, but better still using this other more standard syntax:

strReply=str(getReply)

Finally about the function saveLearned() . If the purpose of this function is to copy the contents of one file into another, it can be much simpler to do so:

import shutil
shutil.copyfile("../data/TEMP.json", "../data/conv.json")

since, regardless of whether the content of the files is json or anything else, all you are doing is copying what is in one file to the other.

Your attempt to use the json module by the middle (and Bryo's response) would cause the resulting conv.json file to end up having a character string with a JSON inside, instead of a valid JSON.

But if you insist on using json , then you should use it both for reading and writing, like this:

def saveLearned():
    temp = open("../data/TEMP.json")
    brain = open("../data/conv.json")
    data = json.load(temp)
    json.dump(brain, data)
    
answered by 18.11.2018 в 01:19