Source code for remarking.cli.commands.json_writer_command
importdatetimeimportjsonimporttypingasTfromtypingimportListfromremarkingimportmodelsfromremarking.cliimportlogfromremarking.cliimportwriteraswriter_fromremarking.cliimportwriter_commanddefjson_serial(obj:T.Any)->T.Any:"""JSON serializer for objects not serializable by default json code."""ifisinstance(obj,(datetime.datetime)):returnint(obj.timestamp())# convert to unix tsraiseTypeError("Type %s not serializable"%type(obj))
[docs]classJSONWriter(writer_.Writer):""" Write a JSON string representing the documents and highlights extracted. The JSON string resembles: .. code-block:: json { "documents": [ { "id": "235c7b66-c048-4639-ae89-8b2d60e3263b", "version": 3, "modified_client": 1626731660, "type": "DocumentType", "name": "Through the Looking Glass", "current_page": 0, "bookmarked": false, "parent": "002de508-09db-4e25-8714-aedf6c484363" } ], "highlights": [ { "hash": "0e0e22dac038605e8ad32d9104525ff767ddf871689fd92a7adcfae4", "document_id": "235c7b66-c048-4639-ae89-8b2d60e3263b", "text": "Alice was sitting curled up in a corner of the great arm-chair", "page_number": 11, "extracted_at": 1626976202, "extraction_method": "RemarkableHighlightExtractor" } ] } :param documents: The list of documents to generate a json string for. :param highlights: The list of highlights to generate a json string for. """def__init__(self,documents:List[models.Document],highlights:List[models.Highlight])->None:self.data={'documents':[doc.to_dict()fordocindocuments],'highlights':[highlight.to_dict()forhighlightinhighlights]}defwrite(self,logger:log.CommandLineLogger)->None:json_string=json.dumps(self.data,default=json_serial)logger.output_result(json_string+'\n')
[docs]classJSONWriterCommand(writer_command.WriterCommand):""" The writer command implementation for the ``json`` output writer. """defname(self)->str:return"json"defoptions(self)->List[writer_command.ClickOption]:return[]deflong_description(self)->str:return"Output highlights and documents as JSON"defshort_description(self)->str:return"Output highlights and documents as JSON"defwriter(self,documents:List[models.Document],highlights:List[models.Highlight],**kwargs:T.Any)->writer_.Writer:returnJSONWriter(documents,highlights)