test-fullautomation / python-jsonpreprocessor

A preprocessor for json files
Apache License 2.0
2 stars 2 forks source link

Namsonx/task/stabi branch #283

Closed namsonx closed 1 month ago

namsonx commented 1 month ago

Hi Son,

the performance issues are solved. Your code looks good. But I cannot find the new output failedJsonDoc (lines starting with "Nearby: "). I am confused.

Have you tested the output?

                if failedJsonDoc is None:
                    print(f"================================= is None")
                    jsonException = f"{error}\nIn file: '{jFile}'"
                else:
                    print(f"================================= is not None")
                    jsonException = f"{error}\nNearby: '{failedJsonDoc}'\nIn file: '{jFile}'"

is None always

            if failedJsonDoc is None:
                print(f"================================= is None (2)")
                jsonException = f"${error}\nIn file: '{jFile}'"
            else:
                print(f"================================= is None (2)")
                jsonException = f"${error}\nNearby: '{failedJsonDoc}'\nIn file: '{jFile}'"

Still ${error} ;-)

Is always not a JSON exception object. Means: .doc is not an element of jsonDecodeError. I have no idea why.

    def __getFailedJsonDoc(jsonDecodeError=None, areaBeforePosition=50, areaAfterPosition=20, oneLine=True):
        failedJsonDoc = None
        if jsonDecodeError is None:
            return failedJsonDoc
        try:
            jsonDoc = jsonDecodeError.doc
        except:
            # 'jsonDecodeError' seems not to be a JSON exception object ('doc' not available)
            print(f"================================= NOT a JSON exception object")
            return failedJsonDoc

No such problems when accessing .doc immediately:

            except Exception as error:
                self.__reset()

                intermediateDocOutput = error.doc
                print(f"================================= intermediateDocOutput (1): {intermediateDocOutput}")

                failedJsonDoc = self.__getFailedJsonDoc(error)
                jsonException = "not defined"
                if failedJsonDoc is None:
                    print(f"================================= is None")
                    jsonException = f"{error}\nIn file: '{jFile}'"
                else:
                    print(f"================================= is not None")
                    jsonException = f"{error}\nNearby: '{failedJsonDoc}'\nIn file: '{jFile}'"
                raise Exception(jsonException)

Assumption: Something went wrong when passing the error to:

failedJsonDoc = self.__getFailedJsonDoc(error)

Still no idea why.

Trying to summarize:

Within an except block we have an exception object available. If we pass this exception object to a method of the same class, we get lost of this object. This in new to me.

We need access to two informations:

jsonDecodeError.doc
jsonDecodeError.pos

Therefore, instead of:

def __getFailedJsonDoc(jsonDecodeError=None, areaBeforePosition=50, areaAfterPosition=20, oneLine=True):

an alternative implementation could be:

jsonDecodeErrorPos = jsonDecodeError.pos
jsonDecodeErrorDoc = jsonDecodeError.doc
def __getFailedJsonDoc(jsonDecodeErrorPos=None, jsonDecodeErrorDoc=None, areaBeforePosition=50, areaAfterPosition=20, oneLine=True):

Hello Holger,

I tried to move the function getFailedJsonDoc out of the CJsonPreprocessor class, then the "Nearby: ..." log appears. So, I think the alternative solution is move this function out of the CJsonPreprocessor class as your first implementation.

Thank you, Son

HolQue commented 1 month ago

Hi Son,

yes, I agree.

HolQue commented 1 month ago

Hi Son,

the self is missing:

def __getFailedJsonDoc(jsonDecodeError=None, areaBeforePosition=50, areaAfterPosition=20, oneLine=True):

Should be

def __getFailedJsonDoc(self, jsonDecodeError=None, areaBeforePosition=50, areaAfterPosition=20, oneLine=True):

o.m.g.

HolQue commented 1 month ago

Success!

The "Nearby" is back :-)